question on fgets

S

subramanian100in

Suppose fgets is used to read a line of input.
char str[1024];
fgets(str, sizeof(str), stdin);
After reading some characters on the same line, if end-of-file is
encountered, will fgets return the 'str' parameter and set EOF
indicator for the stream ? Or will it return the string argument and
set EOF indicator only on subsequent call ?

Kindly clarify

Thanks
V.Subramanian
 
S

santosh

Suppose fgets is used to read a line of input.
char str[1024];
fgets(str, sizeof(str), stdin);

The parenthesis to sizeof is not required except for types.
After reading some characters on the same line, if end-of-file is
encountered, will fgets return the 'str' parameter and set EOF
indicator for the stream ? Or will it return the string argument and
set EOF indicator only on subsequent call ?

Kindly clarify

If end-of-file is detected after one or more characters have been read,
then fgets will return 'str'. Otherwise it will return a null pointer.
 
J

Jens Thoms Toerring

Suppose fgets is used to read a line of input.
char str[1024];
fgets(str, sizeof(str), stdin);
After reading some characters on the same line, if end-of-file is
encountered, will fgets return the 'str' parameter and set EOF
indicator for the stream ? Or will it return the string argument and
set EOF indicator only on subsequent call ?

Here's what the C89 standard says about it:

The fgets function returns s if successful. If end-of-file is
encountered and no characters have been read into the array, the
contents of the array remain unchanged and a null pointer is returned.
If a read error occurs during the operation, the array contents are
indeterminate and a null pointer is returned.

So if the last line of the file has no '\n' at the end, 'str'
gets returned (hitting end-of-file isn't an error) and NULL
only on a subsequent call of fgets().

Regards, Jens
 
C

CBFalconer

Suppose fgets is used to read a line of input.
char str[1024];
fgets(str, sizeof(str), stdin);
After reading some characters on the same line, if end-of-file is
encountered, will fgets return the 'str' parameter and set EOF
indicator for the stream ? Or will it return the string argument
and set EOF indicator only on subsequent call ?

Why ask here? All you have to do is read the C standard, and you
will find the following:

7.19.7.2 The fgets function

Synopsis
[#1]
#include <stdio.h>
char *fgets(char * restrict s, int n,
FILE * restrict stream);

Description

[#2] The fgets function reads at most one less than the
number of characters specified by n from the stream pointed
to by stream into the array pointed to by s. No additional
characters are read after a new-line character (which is
retained) or after end-of-file. A null character is written
immediately after the last character read into the array.

Returns

[#3] The fgets function returns s if successful. If end-of-
file is encountered and no characters have been read into
the array, the contents of the array remain unchanged and a
null pointer is returned. If a read error occurs during the
operation, the array contents are indeterminate and a null
pointer is returned.
 
R

Ralf Damaschke

Suppose fgets is used to read a line of input.
char str[1024];
fgets(str, sizeof(str), stdin);
After reading some characters on the same line, if end-of-file is
encountered, will fgets return the 'str' parameter and set EOF
indicator for the stream ? Or will it return the string argument and
set EOF indicator only on subsequent call ?

After you got three answers that does not address your question I
will give it a try. You already stated in the question that you are
aware that fgets() will return the str parameter (as opposed NULL).(*)

If you thereafter call feof() on this stream it should return true.

7.19.1p2 requires "an end-of-file indicator that records whether
the end of the file has been reached". And by 7.19.10.2p3 "The feof
function returns nonzero if and only if the end-of-file indicator
is set for stream."

(*) I would not expect that fgets returns non-NULL on all systems
for a text file since in text files lines are required to end with
a newline.

-- Ralf
 
R

Richard

CBFalconer said:
Suppose fgets is used to read a line of input.
char str[1024];
fgets(str, sizeof(str), stdin);
After reading some characters on the same line, if end-of-file is
encountered, will fgets return the 'str' parameter and set EOF
indicator for the stream ? Or will it return the string argument
and set EOF indicator only on subsequent call ?

Why ask here? All you have to do is read the C standard

You will of course, in true pompous and big head fashion, be answering
ALL C questions with this ridiculous bit of c.l.c oneupsmanship I
assume?
 
A

Antoninus Twink

You will of course, in true pompous and big head fashion, be answering
ALL C questions with this ridiculous bit of c.l.c oneupsmanship I
assume?

I was pretty impressed that he managed to answer a question about fgets
(albeit very rudely) without spamming about his ridiculous "ggets"
function.
 
R

Richard

Antoninus Twink said:
I was pretty impressed that he managed to answer a question about fgets
(albeit very rudely) without spamming about his ridiculous "ggets"
function.

He did that once today already.
 
V

vippstar

Suppose fgets is used to read a line of input.
char str[1024];
fgets(str, sizeof(str), stdin);

The parenthesis to sizeof is not required except for types.

It is also not required for "types". The parenthesis is required for
the cast.
If end-of-file is detected after one or more characters have been read,
then fgets will return 'str'. Otherwise it will return a null pointer.

Note that in the next read, fgets will return NULL. So it's O.K. to
use fgets like,
while(fgets() != NULL)
 
B

Ben Bacarisse

Suppose fgets is used to read a line of input.
char str[1024];
fgets(str, sizeof(str), stdin);

The parenthesis to sizeof is not required except for types.

It is also not required for "types". The parenthesis is required for
the cast.

The syntax simply says:

unary-expression:
sizeof unary-expression
sizeof ( type-name )

I.e. in the form that needs ( ) they are part of the unary-expression
and they go round a type-name. It is true that a cast-expression is:

cast-expression:
unary-expression
( type-name ) cast-expression

but that does not make the ( type-name ) in the first example a cast.
 
K

Keith Thompson

Joe Wright said:
Ralf said:
Suppose fgets is used to read a line of input.
char str[1024];
fgets(str, sizeof(str), stdin);
After reading some characters on the same line, if end-of-file is
encountered, will fgets return the 'str' parameter and set EOF
indicator for the stream ? Or will it return the string argument and
set EOF indicator only on subsequent call ?
After you got three answers that does not address your question I
will give it a try. You already stated in the question that you are
aware that fgets() will return the str parameter (as opposed NULL).(*)
If you thereafter call feof() on this stream it should return true.
7.19.1p2 requires "an end-of-file indicator that records whether
the end of the file has been reached". And by 7.19.10.2p3 "The feof
function returns nonzero if and only if the end-of-file indicator
is set for stream."
(*) I would not expect that fgets returns non-NULL on all systems
for a text file since in text files lines are required to end with
a newline.

The fgets() function has no such requirement. If the last line of a
text stream has no terminating '\n', so be it. The line in str will be
terminated with '\0' and the eof indicator will be set but str, not
NULL is returned by fgets().

C99 7.19.2p2:

A text stream is an ordered sequence of characters composed into
_lines_, each line consisting of zero or more characters plus a
terminating new-line character. Whether the last line requires a
terminating new-line character is implementation-defined.

If an implementation does require a terminating new-line character on
the last line, and a particular file doesn't have one, then I believe
the behavior of anything that attempts to read from that file is not
defined by the standard.

On most or all systems I've used, the terminating new-line character
is not required, and the behavior of fgets in that case is well
defined.
 
V

vippstar

The syntax simply says:

unary-expression:
sizeof unary-expression
sizeof ( type-name )

I.e. in the form that needs ( ) they are part of the unary-expression
and they go round a type-name. It is true that a cast-expression is:

cast-expression:
unary-expression
( type-name ) cast-expression

but that does not make the ( type-name ) in the first example a cast.

Thanks, you are right. The syntax could be

unary-expression
sizeof unary-expression
sizeof cast-expression

But I guess the standard doesn't like such definitions.
 
C

Chris Torek

The syntax simply says:

unary-expression: [some parts are missing here]
sizeof unary-expression
sizeof ( type-name )

I.e. in the form that needs ( ) they are part of the unary-expression
and they go round a type-name. It is true that a cast-expression is:

cast-expression:
unary-expression
( type-name ) cast-expression

but that does not make the ( type-name ) in the first example a cast.

Thanks, you are right. The syntax could be

unary-expression
sizeof unary-expression
sizeof cast-expression

This would not work right, since then we would have to write, e.g.:

size_t size_of_an_int = sizeof (int) 0;

to get the size of the "int" type. (Note that a cast-expression
that includes a cast -- i.e., that is not simply a unary-expression
to begin with -- consists of a cast *followed by* another
cast-expression, so for the recursion to terminate, the last
cast-expression must consist of a unary-expression, such as the
integer constant 0 in my example.)
But I guess the standard doesn't like such definitions.

It would be possible to factor out the token-sequence "left
parenthesis, type-name, right-parenthesis" into a new nonterminal
such as "cast-prefix", giving:

unary-expr:
postfix-expr
++ unary-expr
-- unary-expr
unary-operator cast-expr
sizeof unary-expr
sizeof cast-prefix

cast-expr:
unary-expr
cast-prefix unary-expr

cast-prefix:
( type-name )

The "cast-prefix" nonterminal would be useful in describing the
syntax for C99's compound literals as well.

(One can also rewrite the entire thing as an operator precedence
grammar, removing the need for many interior nonterminals, but of
course that requires adding operator precedence to the grammar. :) )
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top