12

What does fscanf return when it reads data in the file. For example,

int number1, number2, number3, number4, c;

c = fscanf (spFile, "%d", &number1);
//c will be 1 in this case.

c = fscanf (spFile, "%d %d %d %d", &number1, &number1, &number3, &number4);
//in this case, c will return 4.

I just want to know why it returns such values depending on the number of arguments.

slow
  • 805
  • 3
  • 13
  • 27

5 Answers5

15

From the manpage for the Xscanf family of functions:

Upon successful completion, these functions shall return the number of successfully matched and assigned input items; this number can be zero in the event of an early matching failure. If the input ends before the first matching failure or conversion, EOF shall be returned. If a read error occurs, the error indicator for the stream is set, EOF shall be returned, and errno shall be set to indicate the error

So your first call to fscanf returns 1 because one input item (&number1) was successfully matched with the format specifier %d. Your second call to fscanf returns 4 because all 4 arguments were matched.

Charles Salvia
  • 52,325
  • 13
  • 128
  • 140
  • then does the compiler print EOF when it fails at the first matching? i.e. printf("%d", c); – slow Mar 10 '13 at 02:58
  • 1
    `fscanf` will return `EOF` if it fails before matching any of the arguments. In C `EOF` is an implementation-defined value which you can test for using the macro `EOF`. So you can say `if c == EOF { /* do something */ }` for your return value `c` – Charles Salvia Mar 10 '13 at 03:01
  • 1
    +1 EOF returning works for my question. I want to read a text file from stdio, then write its contents on terminal. `while (fscanf(stdin, "%s", buf)!=EOF){ printf("value read: [%s]\n", buf); }` – EsmaeelE Nov 19 '17 at 01:15
3

I quote from cplusplus.com .

On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.

If a reading error happens or the end-of-file is reached while reading, the proper indicator is set (feof or ferror). And, if either happens before any data could be successfully read, EOF is returned.

--EDIT--

If you are intention is to determine the number of bytes read to a string.

int bytes;
char str[80];
fscanf (stdin, "%s%n",str,&bytes);
printf("Number of bytes read = %d",bytes);
Barath Ravikumar
  • 5,658
  • 3
  • 23
  • 39
1

From the manual page:

*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. *

Hence 1st one returns 1 if able to read one integer from the file, 2nd one returns 4 if able to read 4 integers from the file.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
1

This happens to be a very straight forward question , and has been aptly answered by charles and ed before me. But they didnt mention where you should be looking for such things the next time you get stuck.

first the question -- the fscanf belongs to the family of formated input(scan) functions that are supposed to read a input and report some info on the data read like bytes or the count of items(variable addresses) that got a appropriate input read and had successfull assignment made.

here the fscanf is supposed to check for matches in the input file with the format string provided in the function call and accordingly assign the (in order of their position) variable - address with the value and once completed it will return the total count for the number of successfull assignments it made. hence the result of 1 and next was 4 (assuming input was provided properly).

second part: where to look ? -- well described details for such function are easily found in your manual pages or posix doc if you refer to one.

if you noticed , the previous two answers also contain small extracts from the man pages .

hope this helps.

Nitin4873
  • 16,804
  • 1
  • 13
  • 15
1

The return value is not depending on the number of arguments to fscanf ,it depends on number of values successfully scanned by fscanf.

Hitesh Menghani
  • 977
  • 1
  • 6
  • 14