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


Groups > comp.lang.c > #64815

Re: Fgets question

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Fgets question
Date 2015-07-07 00:57 +0100
Organization A noiseless patient Spider
Message-ID <877fqcu9es.fsf@bsb.me.uk> (permalink)
References <0b5d570e-eed5-4162-b5f9-44b2058639ee@googlegroups.com> <87si91szni.fsf@bsb.me.uk>

Show all headers | View raw


Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

> 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?
>
> This is what usually happens, though the standard says that, for a text
> stream, the implementation may insist on a terminating new-line
> character.  ("Whether the last last line requires a terminating new-line
> character is implementation-defined.")
>
>> And that next time I try
>> fgets, it will return NULL?
>
> Yes.
>
>> So in particular, if fgets(buff, len, fp) returns a non-NULL value,
>> then buff[0] is not nul?
>
> Yes.

This needs to be corrected in the light of Barry Schwarz's better reply:
yes only provided the stream does not have nulls in it.  But if you have
nulls in the stream (specifically at the end) you will have trouble
knowing what stream really contains -- you pretty much have to assume
there are none at the end.  Nulls not at the end of the stream are not a
problem, though it's fiddly to deal with them correctly.

-- 
Ben.

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