C code, seems to be skipping if statements
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.
Try strcmp()
instead of ==
.
if (argv[1] == "-h")
This statement compares two pointers, not two strings. Try:
if (strcmp(argv[1], "-h") == 0)
In C, you need to compare string values using functions like
strcmp(str1, str2);
Don't compare string values using the ==
operator; which checks something completely different.
Compile with "gcc -g -o fr filereader.c", and step through under the debugger (e.g. gdb).
I assure you the compiler isn't "ignoring" your "if" statements.
Do NOT use "argv[1] == "-h". Use "if (strcmp (argv[1], "-h") == 0" instead...
In C you don't compare strings with the ==
operator. Instead you use the following idiom:
if (strcmp(s1, s2) == 0) { ... }
As already mentioned, just understand that "argv[1]" == "-h" would mean you are comparing pointers. Keep this in mind.
And another suggestion I have for you is to look at getopt()
. This is used to parse command line arguments in unix, and will make your life easier and help you in writing robust code.
Try strcmp() instead of == as == compares the address where string values are stored But strcmp compares the content (string values) which is required i think
e.g. int main(){char *a="anshul";char *b="anshul";if(a==b){ printf("ok");} /comapres address where anshul is stored/ if(strcmp(a,b)){printf("ok"); /* this will compare values i.e string anshul therefore always equal but in previous case it depends whether both anshul stored on same memory or not so not a advisable thing to do
精彩评论