473,569 Members | 2,870 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2422
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.comwro te:
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********@yah oo.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********@yah oo.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********@yah oo.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....@gm ail.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

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

Similar topics

4
10255
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 sure that there was a way around it but it escapes me if there is one. Thanks Charles Erwin
6
5597
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 the fgets() before the scanf(). However, this one is not always suitable. Do you know of any other ways of avoiding this problem? -- - Fordi det...
51
8234
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 code? many thx!
2
2333
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 fgets is ignored. I've been even tempted to use gets. Please,
35
9914
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 the program. Since fgets() doesn't return the number of characters read it is pretty tough to handle the embedded nulls once they are in the buffer....
11
3648
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 EOF/error), returns n-1 characters plus a appended null character.
8
2318
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 calculates loan amortization schedules, writes the data to a text file, and then upon user prompt, the program will display the created file and...
42
6759
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 from stream and stores them in string until (num -1) characters have been read or a newline or EOF character is reached, whichever comes first." ...
26
2242
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 string literal. Strlen was what I used to decide the \0 was not added. How can it be added for a string? My code. int main (void) { char input;...
0
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7679
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5223
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.