[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: [OT] Help with glibc memory streams



Enrique Robledo Arnuncio wrote:
> 
> It seems to be fgets fault, but is it a bug really?
> 
> from the info manual (fgets),
> 
>      The `fgets' function reads characters from the stream STREAM up to
>      and including a newline character and stores them in the string S,
>      adding a null character to mark the end of the string.
>      [...]
>      If the system is already at end of file when you call `fgets', then
>      the contents of the array S are unchanged and a null pointer is
>      returned.  A null pointer is also returned if a read error occurs.
>      Otherwise, the return value is the pointer S.
> 
> So, it does not say what happens if fgets find an end of file before
> finding the newline character.

It's implied. As you know there are separate indicators in the stream
implementation [IIRC ;) ] one is EOF, and another is error.
It's not an error to read a line up to EOF. That's a common application
and anyone who wrote some kind of a parser would acknowledge that.

In the case "fgets finds an EOF before newline", the EOF flag should
be set but the error flag certainly not. That's what happens in _one_
of the examples that I attached. _Please_ compile and run them [with
the test file I supply] before you post.

> I guess that's why text files not ending with an end of line are
> sometimes problematic.... Maybe you should follow the info advice, and
> try getline instead.

You're forgetting that getline is a GNU extension[1], and btw try
using getline instead and see what happens! I'm talking about
an actual discrepancy that shouldn't occur, I read the info
docs many times [and K&R a few times in my youth ;)]

Looking forward to the results of your tests!

[1] I'd use getline if I could, but as I say just try that with
memory streams and see what happens. The obstacle for me is that
the project I work on should work on proprietary systems which
lack GNU libc, too. And I don't think we can make assumptions about
any system. :( Maybe this needs some autoconf tests!

-- 
Eray (exa) Ozkural
Comp. Sci. Dept., Bilkent University, Ankara
e-mail: erayo@cs.bilkent.edu.tr
www: http://www.cs.bilkent.edu.tr/~erayo



Reply to: