问题描述:

There is more to this code obviously but I am just curious as to what this line of code actually does. I know the while loop and such but am new to the fscanf()

while (fscanf(input_file, "%s", curr_word) == 1)

网友答案:

fscanf() returns the number of input items successfully scanned and stored.

as per the man page

Return Value

These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.

In your case

while (fscanf(input_file, "%s", curr_word) == 1)

fsaacf() will return a value of 1 if it is able to successfully scan a string (as per the %s format specifier) from input_file and put it into curr_word.

网友答案:

fscanf(input_file, "%s", curr_word) reads the input stream input_file and stores the next sequence of non spacing characters into the array pointed to by cuur_word and appends a '\0' byte. As you can see, the size of this array is not passed to fscanf. This is a classical case of potential buffer overflow, a security flaw that can be exploited by a hacker by storing appropriate contents in the input stream.

After gets, the scanf family of library functions is the best source of buffer overflow bugs one can find.

It is very difficult to use fscanf correctly. Most C programmers should avoid it.

相关阅读:
Top