1

When I used the fscanf function in the while loop i get the error; "use of undeclared identifier "word". Heres my while loop:

while(fscanf(fpt, "%s", word)!= EOF)

So first I declared my "word" variable as

char word[LENGTH];

in the row above and got no error. But i'm not quite sure if that would be the correct way to do it, using the data type char? Because the fscanf function looks for a string "%s" and stores that string in a placeholder "word". Would it be more correct to use

string word = NULL;?

Tried to just use "string word;" but got an error telling me to initialize the word variable to NULL. So I did, and now I got no errors, but I'm not 100 % sure if it is correct?

Any help would be highly appreciated!

4
  • Is the word declared inside the function load? – MARS Mar 18 '17 at 20:19
  • At first it was not and therefore i got the "undeclared identifier" error. But then I declared it as "char word[LENGTH];" and I got no error anymore. But now I wonder what is the right way to declare it? As char word[LENGTH] or as string word = NULL; ? – L.D Mar 18 '17 at 20:33
  • if we say "string word", we are actually declaring a pointer, so the compiler yells at us to initialize it as NULL. For me it would be more convenient to declare it as char word [LENGTH +1], we avoid problems, since declaring word as a pointer can lead us to use malloc, for me it is simpler the first form, but you must know that both forms are possible . LENGTH +1 is required for the end-of-string character, be aware that LENGTH is the maximum length, not counting the end-of-string character – MARS Mar 18 '17 at 20:56
  • Allright, now I understand :) Thank you very much for your help! – L.D Mar 18 '17 at 21:20
2

Do not use string word = NULL. fscanf will not "put" anything in it because NULL is a defined constant, and it will not give an error. You could end up in an infinite loop (it also will not detect EOF).

Declaring word as a char array is a fine approach. Remember 45 letters will "fit" in the array declared char word[45], but the "%s" directive of fscanf will try to make it a valid string, and will therefore need one more byte for the null terminator.

Another option would be to use the C type char * and allocate it with malloc or calloc.


In the interest of completeness, there is a "more comfortable" approach. You can use the "m" option in the conversion specification, and fscanf will allocate the memory; it is up to the programmer to free it. Something like this:

string word;
while(fscanf(infile,"%ms",&word) != EOF)
{
    // do "word" logic
    free(word);
}

From man fscanf:

An optional 'm' character. This is used with string conversions (%s, %c, %[), and relieves the caller of the need to allocate a corresponding buffer to hold the input: instead, scanf() allocates a buffer of sufficient size, and assigns the address of this buffer to the corresponding pointer argument, which should be a pointer to a char * variable (this variable does not need to be initialized before the call). The caller should subse‐ quently free(3) this buffer when it is no longer required.

3
  • Interesting! Thanks for the useful input :) – L.D Mar 18 '17 at 23:03
  • +1 fscanf will not "put" anything in it because NULL is a defined constant can you please elaborate more on this. Why does this not throw an error ? I can't get my head around this. Shouldn't it throw segfault here ? – vadasambar Aug 23 '17 at 15:13
  • That's how the function is implemented. It is considered a matching failure because "Each pointer argument must be of a type that is appropriate for the value returned by the corresponding conversion specification." (from the man page). It cannot fulfill the directive (because it cannot "store" into NULL), so it fails, gracefully. Function will return 0. – DinoCoderSaurus Aug 23 '17 at 18:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .