473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

question on fgets

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
Jul 27 '08 #1
14 2403
su**************@yahoo.com, India wrote:
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.

Jul 27 '08 #2
su**************@yahoo.com, India <su**************@yahoo.comwrote:
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
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Jul 27 '08 #3
"su**************@yahoo.com, India" wrote:
>
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.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

Jul 27 '08 #4
su**************@yahoo.com, India wrote:
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
Jul 27 '08 #5
CBFalconer <cb********@yahoo.comwrites:
"su**************@yahoo.com, India" wrote:
>>
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?
Jul 27 '08 #6
Ralf Damaschke wrote:
su**************@yahoo.com, India wrote:
>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().

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jul 27 '08 #7
On 27 Jul 2008 at 18:26, Richard wrote:
CBFalconer <cb********@yahoo.comwrites:
>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?
I was pretty impressed that he managed to answer a question about fgets
(albeit very rudely) without spamming about his ridiculous "ggets"
function.

Jul 27 '08 #8
Antoninus Twink <no****@nospam.invalidwrites:
On 27 Jul 2008 at 18:26, Richard wrote:
>CBFalconer <cb********@yahoo.comwrites:
>>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?

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.
Jul 27 '08 #9
On Jul 27, 3:42 pm, santosh <santosh....@gmail.comwrote:
subramanian10...@yahoo.com, India wrote:
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.
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.
Note that in the next read, fgets will return NULL. So it's O.K. to
use fgets like,
while(fgets() != NULL)

Jul 28 '08 #10
vi******@gmail.com writes:
On Jul 27, 3:42 pm, santosh <santosh....@gmail.comwrote:
>subramanian10...@yahoo.com, India wrote:
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.

--
Ben.
Jul 28 '08 #11
Joe Wright <jo********@comcast.netwrites:
Ralf Damaschke wrote:
>su**************@yahoo.com, India wrote:
>>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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 28 '08 #12
On Jul 28, 4:51 am, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
vipps...@gmail.com writes:
On Jul 27, 3:42 pm, santosh <santosh....@gmail.comwrote:
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.
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.
Jul 28 '08 #13
>On Jul 28, 4:51 am, Ben Bacarisse <ben.use...@bsb.me.ukwrote:
>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.
In article <9e**********************************@2g2000hsn.go oglegroups.com>
<vi******@gmail.comwrote:
>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. :-) )
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: gmail (figure it out) http://web.torek.net/torek/index.html
Jul 28 '08 #14
In article <g6*********@news5.newsguy.com>
On Jul 28, 2:55 pm, Chris Torek <nos...@torek.netwrote:
<snip>

Thanks, quite informative.
Jul 28 '08 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Charles Erwin | last post by:
Is there any way, upon scanning in a file line by line to avoid missing the last line if there is not a newline character (aka you have to hit return on the last line of input in your file). I was...
6
by: Eirik | last post by:
Hey, all groovy C programmers, I've read in the FAQ(question 12.18) about how applications skip calls to (f)gets() after scanf() has been used. How can I avoid this? I know that I can by putting...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
2
by: Diego | last post by:
Hi, Using gcc 2.96 This message was suggested by a thread started by Knak on 21/03/04 The question is: When I run the following code, if I want to introduce a second pile of data, the...
35
by: David Mathog | last post by:
Every so often one of my fgets() based programs encounters an input file containing embedded nulls. fgets is happy to read these but the embedded nulls subsequently cause problems elsewhere in...
11
by: santosh | last post by:
Hi, A book that I'm currently using notes that the fgets() function does not return until Return is pressed or an EOF or other error is encountered. It then at most (in the absence of...
8
by: AG | last post by:
Hello, This is my first post to this group, and on top of that I am a beginner. So please direct me to another group if this post seems out of place.... I have recently written a program which...
42
by: mellyshum123 | last post by:
I need to read in a comma separated file, and for this I was going to use fgets. I was reading about it at http://www.cplusplus.com/ref/ and I noticed that the document said: "Reads characters...
26
by: Bill Cunningham | last post by:
I was talking with someone about fgets and he said that fgets puts the \n in a string but not \0. I decided to test this assumption because my documentation didn't say if fgets put \0 after a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.