Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Fgets question |
| Date | 2015-07-06 16:29 -0700 |
| Organization | None to speak of |
| Message-ID | <lnpp44c1bq.fsf@kst-u.example.com> (permalink) |
| References | <0b5d570e-eed5-4162-b5f9-44b2058639ee@googlegroups.com> |
Paul N <gw7rib@aol.com> writes:
> I just had a moment of doubt about fgets, and rather to my surprise,
> neither MSDN or the C FAQ seemed to answer my question.
>
> If I read a file using fgets, which doesn't end with a newline, am I
> right in thinking that on attempting to read the last line, the
> function will read in the characters that are there, append a nul but
> no newline, and return a non-NULL value? And that next time I try
> fgets, it will return NULL?
Yes, generally. I believe the description of the fgets() function in
the standard makes this reasonably clear (N1570 7.21.7.2p2):
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.
p3:
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 in particular, if fgets(buff, len, fp) returns a non-NULL value,
> then buff[0] is not nul?
Yes, I believe that's correct.
*However* the description of text streams in 7.21.2 says that a trailing
new-line may be required. (By implication, if there is no new-line at
the very end of the file, the behavior is undefined.)
Data read in from a text stream will necessarily compare equal
to the data that were earlier written out to that stream only
if: the data consist only of printing characters and the control
characters horizontal tab and new-line; no new-line character is
immediately preceded by space characters; and the last character
is a new-line character.
On Unix-like systems, and *I think* on Windows systems, omitting the
newline at the end of the last line is acceptable.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Working, but not speaking, for JetHead Development, Inc.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Fgets question Paul N <gw7rib@aol.com> - 2015-07-06 14:06 -0700
Re: Fgets question Barry Schwarz <schwarzb@dqel.com> - 2015-07-06 15:09 -0700
Re: Fgets question Keith Thompson <kst-u@mib.org> - 2015-07-06 16:41 -0700
Re: Fgets question Paul N <gw7rib@aol.com> - 2015-07-07 15:16 -0700
Re: Fgets question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-06 23:13 +0100
Re: Fgets question Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-07-07 00:57 +0100
Re: Fgets question Keith Thompson <kst-u@mib.org> - 2015-07-06 16:29 -0700
Re: Fgets question Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-11 05:16 -0700
Re: Fgets question Keith Thompson <kst-u@mib.org> - 2015-07-11 14:04 -0700
Re: Fgets question Richard Damon <Richard@Damon-Family.org> - 2015-07-11 19:56 -0400
Re: Fgets question Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-15 09:26 -0700
Re: Fgets question Keith Thompson <kst-u@mib.org> - 2015-07-15 10:42 -0700
Re: Fgets question Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-20 10:53 -0700
Re: Fgets question Keith Thompson <kst-u@mib.org> - 2015-07-20 12:51 -0700
Re: Fgets question Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-24 10:10 -0700
Re: Fgets question Keith Thompson <kst-u@mib.org> - 2015-07-24 12:15 -0700
Re: Fgets question Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-26 16:41 -0700
Re: Fgets question Keith Thompson <kst-u@mib.org> - 2015-07-27 11:42 -0700
Re: Fgets question Tim Rentsch <txr@alumni.caltech.edu> - 2015-07-30 15:42 -0700
csiph-web