0
int main()
{
FILE *f;
f=fopen("words.txt","r");
char wd[10];
string word;
printf("Enter a word to be searched\n");
word=GetString();
while (!feof(f))
{
    fscanf(f,"%s",wd);   
    //printf("%s\n",wd);
    if(strcmp(word,wd)==0)
    {
        printf("Yes\n");
        break;
    }

 }
 fclose(f);
 return 0;
}

If the word to be searched, is present in the file, it is giving the correct answer- YES, otherwise, its giving segmentation fault(core dumped). Please help!!

Maxim Makhun
  • 2,197
  • 1
  • 22
  • 26
tanmay2893
  • 123
  • 1
  • 12
  • 1
    What is `string` and what `GetString` does ? – P0W Jan 05 '14 at 15:10
  • 1
    No `string` type in C. – haccks Jan 05 '14 at 15:10
  • 2
    http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong – Mat Jan 05 '14 at 15:11
  • You can ignore that, it functions as it should. It gets the string by including a preprocessor (used in some course). – tanmay2893 Jan 05 '14 at 15:12
  • 2
    `char wd[10];` : There is a large enough? – BLUEPIXY Jan 05 '14 at 15:14
  • yes, it is large enough. I am checking for 'mr' and it is giving that error. If i check for 'me', its giving YES. – tanmay2893 Jan 05 '14 at 15:16
  • Why aren't you checking the return value of `fscanf`? Is it possible that `GetString` returned a `NULL` pointer, which would cause a segfault on the strcmp line? What warnings does your compiler emit? You are compiling with something like -Wall for GCC or /Wall for Visual C++ to see all warnings, right? –  Jan 05 '14 at 15:18
  • GetString would input a string to be searched, else, it will itself give a error. – tanmay2893 Jan 05 '14 at 15:20
  • Just read the accepted answer from @mat's link. You read one more time than you want to, and should expect a segfault. – gnometorule Jan 05 '14 at 15:21
  • Making it a do-while loop instead should fix it (although it's not pretty). – gnometorule Jan 05 '14 at 15:25
  • "it is large enough. I am checking for 'mr'" --- you should be telling that to fscanf, not to us. – n. m. could be an AI Jan 05 '14 at 15:28
  • You can ignore this Seg fault too, move on, try something else :P – P0W Jan 05 '14 at 15:35
  • @gnometorule do-while is not working. – tanmay2893 Jan 05 '14 at 15:39
  • Well, it should. :) Think where you could have a segfault: (1) wd overflows (you say that won't happen for your data), or (2) you read past EOF. Assuming you don't feed your program an empty file, do-while handles (2). If (1) is really no issue, and do-while doesn't solve (2), your understanding of the situation (string, getString, data size, etc) isn't quite what you think it is. Just start up gdb (if you work under Linux), and single step through processing a toy file with only 2 words. You'll see where you segfault. If you use Mac/windows, use your favorite debugger on them. – gnometorule Jan 05 '14 at 16:05

3 Answers3

0

Line 6: string word; There is no string data type in C. And, Line 8: word = GetString(); You didn't include definition of function. Assuming it returns pointer to a string, but won't work anyway because line 6 will not compile.

Use: char word[10] if your sure word won't be longer than 9 characters.

Dave Black
  • 65
  • 8
0

@gnometorule was correct. You're reading one more time than you need to. If you checked your return value, you would find fscanf probably returned EOF. What you want to do is read the file as long as fscanf returns an expected value. Since you are reading 1 item, it should return 1. If it doesn't, there is a problem. Moreover, feof will only be true if the end of the file was reached. What if there was a read error? You may be trying to read from a file that has its error indicator set, and feof won't detect that.

Review the following code. It should give you an idea of how to handle files.

/* This is used to determine whether an end-of-file error message should be printed or the word was found. */
int cmp_result;

/* Stop reading when the file can't be read or the words are equal. */
do
    cmp_result = strcmp(word, wd);
while (fscanf(f, "%s", wd) == 1 && cmp_result != 0);

/* Check for a file read error. */
if (ferror(f))
    printf("Error reading file\n");

/* End of file reached before words are equal; feof(f) may be true when cmp_result==0 or not, so checking cmp_result is more reliable in this case. */
else if (cmp_result != 0)
    printf("Word '%s' not found\n", word);

/* Words are equal. */
else
     printf("Yes\n");

The logic is that if the loop was exited, there was a read error or the word was found. The branching statements after the loop test this.

First, the file's error indicator is checked. If it isn't set, the end of the file was reached, or the word was found.

If the word was found, the end of file indicator may be set or not. But if the word wasn't found, the end of file indicator must be set, else the loop wouldn't have exited since we already ruled out the error indicator in the first if statement. This is why we check the comparison result instead of using feof(f).

If those cases fail, we are left with one possibility: the word was found.

I hope this helps!

0

Anytime while (!feof(f)) is in your code, it almost means that you are in the wrong place. feof() will only be set after reading the end of the stream. It does not indicate, that the next read will be the end of the stream. See here for more info.

Replace

while (!feof(f))
{
    fscanf(f,"%s",wd);  
    ... ...
}

by

while (fscanf(f,"%s",wd))
{
    ... ...
}
Community
  • 1
  • 1
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174