return value of fprintf
olli at lurza.secnetix.de
Tue Jan 23 13:40:09 UTC 2007
K?i?tof ?elechovski <giecrilj at stegny.2a.pl> wrote:
> I have run the code in a loop and the return value is periodically -1.
That's to be expected. Once in a while, the fprintf() will
fill the stdio buffer, causing it to be flushed. If an
error occurs during the flushing, it will be noticed and
returned. That's the "if an error was encountered" case
from the standard's wording. ;-)
Basically, you can think of fprintf() calling fputc()
repeatedly, which simply adds characters to the stdio
buffer until it is full. If it's full, it gets flushed,
and only then an actual I/O operation occurs, and only
then an actual I/O error can occur.
> Interestingly enough,
> although the stream error indicator is persistent,
> it is not taken into account to prevent data corruption as in C++ streams.
Can you describe that in more detail, please? Maybe
give a code example? I don't see how data corruption
could occur if your code is correct.
> The reason may be that POSIX streams must set errno on failure
> (this is *not* required by the ANSI standard)
> but there is no errno value to indicate that the error condition is set
> (except EBADF -
> but the POSIX standard should be modified to allow this extension,
> since at present it only means "Bad file descriptor").
I'm sorry I don't understand what you mean. When the
error indicator is set, errno is always set to an
appropriate value (e.g. ENOSPC if you run out of space
on the file system). If you have an error condition
on s stream, but you ignore it and continue to perform
I/O on the stream without removing the cause of the
problem, you're on your own. You'll get undefined
Oliver Fromme, secnetix GmbH & Co. KG, Marktplatz 29, 85567 Grafing
Dienstleistungen mit Schwerpunkt FreeBSD: http://www.secnetix.de/bsd
Any opinions expressed in this message may be personal to the author
and may not necessarily reflect the opinions of secnetix in any way.
(On the statement print "42 monkeys" + "1 snake":) By the way,
both perl and Python get this wrong. Perl gives 43 and Python
gives "42 monkeys1 snake", when the answer is clearly "41 monkeys
and 1 fat snake". -- Jim Fulton
More information about the freebsd-standards