473,699 Members | 2,734 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
14 2434
vi******@gmail. com writes:
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.
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********@com cast.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_Keit h) 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....@gm ail.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************ *************** *******@2g2000h sn.googlegroups .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*********@ne ws5.newsguy.com >
On Jul 28, 2:55 pm, Chris Torek <nos...@torek.n etwrote:
<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
10289
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
5602
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 rotar til måten folk vanlegvis les tekst på.
51
8268
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
2341
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
9977
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. So two questions: 1. Why did the folks who wrote fgets() have a successful
11
3661
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
2323
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 print the file. Until this morning, everything worked fine, and then I started messing around with...
42
6800
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." My question is that if it stops at a new line character (LF?) then how does one read a file with...
26
2266
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; fgets (input, sizeof(input), stdin); printf ("%i\n", strlen(input));
0
8685
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8612
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9171
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8905
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7743
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5869
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4373
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2342
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.