scanf()/fscanf() question

I

Ioannis Vranos

In K&R2 fscanf() is reported to return EOF in case of error. However
when I use it in the style:

fscanf(stdin, "%d", &x)

and enter a character, it returns 0 and not EOF. When which
circumstances does it return EOF and when 0?
 
M

Malcolm McLean

Ioannis Vranos said:
In K&R2 fscanf() is reported to return EOF in case of error. However when
I use it in the style:

fscanf(stdin, "%d", &x)

and enter a character, it returns 0 and not EOF. When which circumstances
does it return EOF and when 0?
EOF for some sort of physical / software problem with stdin. 0 if stdin is
working fine but the integer not read in correctly, 1 or the number of
arguments successfully converted when everything is fine.
 
S

santosh

Ioannis said:
In K&R2 fscanf() is reported to return EOF in case of error. However
when I use it in the style:

fscanf(stdin, "%d", &x)

and enter a character, it returns 0 and not EOF. When which
circumstances does it return EOF and when 0?

EOF will be returned if the stream is at end-of-file or an error occurs
before any conversions or a matching failure are performed.

In this case I believe your call to fscanf will return EOF if you enter
the system's end-of-file key sequence (CTRL-D for UNIX and CTRL-Z for
Windows). Can you test it?

Otherwise the count of items that were successfully translated and
assigned will be returned, which in this case is zero, since fscanf is
looking for a numeric character sequence.
 
C

Chris Torek

In K&R2 fscanf() is reported to return EOF in case of error. However
when I use it in the style:

fscanf(stdin, "%d", &x)

and enter a character, it returns 0 and not EOF. When which
circumstances does it return EOF and when 0?

The scanf engine -- the thing that implements scanf and fscanf (and
even sscanf except that some concepts no longer apply) -- has two
kinds of "failure": an "input failure" and a "matching failure".
(This is spelled out in the C Standard, so all implementations
do this, or at least, are required to work "as if" they had the
two kinds of failure.)

"Input failure" occurs when getc(stream) returns EOF. This in
turn implies that feof(stream) or ferror(stream) (or perhaps even
both) will be nonzero.

"Matching failure" occurs only when getc(stream) succeeds, but
the character obtained cannot be matched against the current
directive.

The return value from fscanf() is EOF only in the event of input
failure, and even then, only if the input failure occurs before
any "conversion". Conversions are the things introduced with "%"
(apparently including %n).

A question for the Standards Interpreters: if the format argument
to a scanf-family directive *begins* with %n, does that mean that
this call can never return EOF? For instance, is:

int x;
char c;

while (scanf("%n%c", &x, &c) != EOF)
continue;

always an infinite loop? (The reasoning here is that %n is a
successful conversion, always storing 0 into x, and then the %c
"eats" characters until EOF -- but at that point, each EOF occurs
after one %n "conversion", since %n does not start by reading
characters from the stream.)
 
B

Ben Bacarisse

Chris Torek said:
The return value from fscanf() is EOF only in the event of input
failure, and even then, only if the input failure occurs before
any "conversion". Conversions are the things introduced with "%"
(apparently including %n).

A question for the Standards Interpreters: if the format argument
to a scanf-family directive *begins* with %n, does that mean that
this call can never return EOF? For instance, is:

int x;
char c;

while (scanf("%n%c", &x, &c) != EOF)
continue;

always an infinite loop? (The reasoning here is that %n is a
successful conversion, always storing 0 into x, and then the %c
"eats" characters until EOF -- but at that point, each EOF occurs
after one %n "conversion", since %n does not start by reading
characters from the stream.)

Interesting, but that view is a real stretch! The term "conversion"
and "conversion specifier" are rather overused in 7.19.6.2 (fscanf)
but in paragraph 10 we get a clear statement that nether '%' nor 'n'
(i.e. %% and %n) involve a conversion. So although %% and %n are
conversion specifiers, neither "converts" anything.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top