Path: csiph.com!optima2.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Fgets question Date: Mon, 06 Jul 2015 16:29:13 -0700 Organization: None to speak of Lines: 52 Message-ID: References: <0b5d570e-eed5-4162-b5f9-44b2058639ee@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="21994"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+z2BwJl7KvlslUayZHm1lI" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:ssWcjakvq8awRuUbuyoA6XZMSIU= sha1:w4+LMPqqP4ZSMa74SSh28baRpd8= Xref: csiph.com comp.lang.c:64812 Paul N 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 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"