So, I tried to find a solution for this, but I can really find messages in which a new line or zero byte is missing from one of the lines. I am absolutely sure that this is not so.
I use the following function to compare a word with a file containing a list of words with one word on each line (a dictionary in a function). Here is the code:
int isWord(char * word,char * dictionary){ FILE *fp; fp = fopen(dictionary,"r"); if(fp == NULL){ printf("error: dictionary cannot be opened\n"); return 0; } if(strlen(word)>17){ printf("error: word cannot be >16 characters\n"); return 0; } char longWord[18]; strcpy(longWord,word); strcat(longWord,"\n"); char readValue[50] = "a\n"; while (fgets(readValue,50,fp) != NULL && strcmp(readValue,longWord) != 0){ printf("r:%sw:%s%d\n",readValue,longWord,strcmp(longWord,readValue));
The code compiles without errors, and the function perfectly reads the dictionary file and prints a list of words as they appear. The problem I am facing is that even when the two lines are identical, strcmp does not return 0, so the function returns false for any input.
for example, I get:
r:zymoscope w:zymoscope -3
Any ideas? I feel like I should be missing out on something obvious, but I could not find anything in my quest.
Xephz source share