Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #64812

Re: Fgets question

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 <kst-u@mib.org>
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 <lnpp44c1bq.fsf@kst-u.example.com> (permalink)
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

Show key headers only | View raw


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


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