Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6634883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:58:58+00:00 2026-05-25T22:58:58+00:00

First and foremost, I am not asking anyone to write a program for me

  • 0

First and foremost, I am not asking anyone to write a program for me or to do my homework for me… I have an error and I can’t figure out what’s causing it, and where to look to fix it.

So I’m attempting to create a program that will take a command and an input file from the command line, and, based on the command selection, perform one of several functions: either coutn the lines of the input file, count the words of the input file, or check the input file for palindromes and then compare the palindromes (if any) to a dictionary file.

Here is the code as it stands:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>

void usage(char *prog_name) //usage file if input format is wrong
{
    printf("Usage: %s, <-h>|<-l>|<-w>|<-p>, <filename>\n", prog_name);
    exit(0);
}

void help(char *prog_name, char *filename) //help file for -h option
{
    printf("Welcome to the help file\n");
    printf("For number of lines, enter: <%s>, <-l>, <%s>.\n", prog_name, filename);
    printf("For number of words, enter: <%s>, <-w>, <%s>.\n", prog_name, filename);
    printf("To list the palindromes in alphabetical order, print the number of
    palindromes, and to check them against the dictionary, enter: <%s>, <-p>, <%s>\n",
    prog_name, filename);
    exit(0);
}

void fatal(char *);  //function for fatal errors
void *ec_malloc(unsigned int);  //error-checked malloc wrapper

void linecount(char *filename)  // counts the number of lines of input file
{
    char *infile;
    infile = (char *) ec_malloc(strlen(filename));
    strcpy(infile, filename);
    FILE *fp = fopen(infile, "r");

    if (fp == NULL)         
         fatal("input file does not exist");

    int ch;
    int count = 0;                                  

    do {             
    ch = fgetc(fp);
    if( ch== '\n')
            count++;
    }   
    while( ch != EOF );                                 

    printf("Total number of lines %d\n",count);
    exit(0);            
}

int main(int argc, char *argv[])
{
    if (argc < 3) //if there aren't enough arguments, display the usage file
    {
            usage(argv[0]);
    }
    else if (argv[1] == "-h") //this is the help option
    {
        help(argv[0], argv[2]);
    }
    else if (argv[1] == "-l") //this is the line count option
    {
        linecount(argv[2]);
    }
    else
    {

            fatal("skipped if functions");
    }
    return 0;
}
void fatal(char *message) //this function displays an error message and then exits
{   
    char error_message[100];
    strcpy(error_message, "[!!] Fatal Error ");
    strncat(error_message, message, 83);
    perror(error_message);
    exit(-1);
}

void *ec_malloc(unsigned int size) //wrapper function for an error checked malloc
{
    void *ptr;
    ptr = malloc(size);

    if(ptr == NULL)
        fatal("in ec_malloc() on memory allocation");

    return ptr;
}   

So, when I run:

gcc -o fr filereader.c

./fr -h fevbwervrwe.txt

I get the error message stating that the if statements were skipped.

Same if I try running

./fr -l vrweqvervvq.txt

It seems the compiler is skipping over my if statements, and I honestly can’t figure out why. Any help at all would be greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:59:00+00:00Added an answer on May 25, 2026 at 10:59 pm

    Try strcmp() instead of ==.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First and foremost, I have tried searching and can't really find anything that will
First and foremost, I do not know RegEx but am trying to piece something
First and foremost thank you for checking out my problem, and for any help
First and foremost, apologies for any cross-posting. Hope I'm not repeating an issue here,
First and foremost: JSON and XML are not an option in this specific case,
First and foremost-- I have a file of strings. The smallest file is about
First and foremost please don't scream Use an IDE at me. I have given
Hi first and foremost not the type of guy to expect people to hand
First and foremost, I apologize for any vagueness in this question. At this point,
First and foremost, I've been learning JS for about 2-3 hours. I've lost track.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.