fstream, FILE* and errno 
Author Message
 fstream, FILE* and errno

Quote:


> > May fstream fail and not set errno?
> > Does fstream depend on free stdio streams?

> If a library function that returns errno error codes fails, then errno
> definately contains the error value.

1. Library function do not return errno codes.  They set the global
   value errno instead.

2. Most functions are not guaranteed by the C Standard to set errno on
   failure.

Quote:
> If however the function succeeds, then errno is undefined.

Not true.  errno is zero at program startup and there are no library
functions that set it to an undefined value.  errno is always
readable, regardless of whether there was an error.

Quote:
> Using errno after the function failed is safe.

Using errno is always `safe'.  What could go wrong?


Mon, 01 Apr 2002 03:00:00 GMT  
 fstream, FILE* and errno

 >> If however the function succeeds, then errno is undefined.

 Ben> Not true.  errno is zero at program startup and there are no
 Ben> library functions that set it to an undefined value.  errno is
 Ben> always readable, regardless of whether there was an error.

You're (deliberately or otherwise) missing the point, which is that
errno can contain either a zero or non-zero value (and hence is
undefined) after a call to a function which succeeds.

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>



Mon, 01 Apr 2002 03:00:00 GMT  
 fstream, FILE* and errno

Quote:


>  >> If however the function succeeds, then errno is undefined.

>  Ben> Not true.  errno is zero at program startup and there are no
>  Ben> library functions that set it to an undefined value.  errno is
>  Ben> always readable, regardless of whether there was an error.

> You're (deliberately or otherwise) missing the point, which is that
> errno can contain either a zero or non-zero value (and hence is
> undefined) after a call to a function which succeeds.

That is not the meaning of the word "undefined" when used in an
ANSI C context.

Richard



Mon, 01 Apr 2002 03:00:00 GMT  
 fstream, FILE* and errno

Quote:



> >  >> If however the function succeeds, then errno is undefined.

> >  Ben> Not true.  errno is zero at program startup and there are no
> >  Ben> library functions that set it to an undefined value.  errno is
> >  Ben> always readable, regardless of whether there was an error.

> > You're (deliberately or otherwise) missing the point, which is that
> > errno can contain either a zero or non-zero value (and hence is
> > undefined) after a call to a function which succeeds.

> That is not the meaning of the word "undefined" when used in an
> ANSI C context.

The word "undefined" has no definition specific to ANSI C.  Section 1.6
of the standard, Definitions of Terms, does give a definition for the
term "undefined behavior", but not for "undefined" when used alone.

Sent via Deja.com http://www.deja.com/
Before you buy.



Thu, 04 Apr 2002 03:00:00 GMT  
 fstream, FILE* and errno

Quote:


> > If however the function succeeds, then errno is undefined.

> Not true.  errno is zero at program startup and there are no library
> functions that set it to an undefined value.  errno is always
> readable, regardless of whether there was an error.

As always, I hesitate to quibble with Ben Pfaff.  However, this seems
to be a case where a quibble is called for.

Section 4.1.3 of the standard says, in part:

  The value of errno is zero at program startup, but is never set to
  zero by any library function.  The value of errno may be set to
  nonzero by a library function call whether or not there is an
  error, provided the use of errno is not documented in the
  description of the function in the standard.

So, library functions for which the standard does not document the
use of errno (most library functions, that is) may set errno to
some nonzero value regardless of whether they fail or succeed.

Perhaps Ben's problem was with the phrase "... then errno is
undefined."  I took the poster's usage of this phrase to have
been intended to mean "... perhaps the value of errno is undefined,"
but I see on re-reading the phrase that it could have been taken to
mean "... perhaps errno itself is undefined (and, by implication,
unreadable)".  Ben seems to have taken it this way.  Of course,
errno is defined in errno.h as a modifiable lvalue that has a
type int.  The success or failure of a library function has no
bearing on whether or not errno itself is defined.

Perhaps Ben's problem was with the use of the term "undefined."  I
see at least one other response on this thread saying that this
term is used with a different meaning than it ought to have
when used in the ANSI C context.  However, checking my copy of the
standard, I see no definition there for the term "undefined."
There is a definition for the term "undefined behavior," but not
for the term "undefined."

Sent via Deja.com http://www.deja.com/
Before you buy.



Thu, 04 Apr 2002 03:00:00 GMT  
 fstream, FILE* and errno

Quote:




> > > If however the function succeeds, then errno is undefined.

> > Not true.  errno is zero at program startup and there are no library
> > functions that set it to an undefined value.  errno is always
> > readable, regardless of whether there was an error.

> As always, I hesitate to quibble with Ben Pfaff.  However, this seems
> to be a case where a quibble is called for.

[...]
> Perhaps Ben's problem was with the phrase "... then errno is
> undefined."

Yes.

Quote:
> I took the poster's usage of this phrase to have been intended to
> mean "... perhaps the value of errno is undefined," but I see on
> re-reading the phrase that it could have been taken to mean
> "... perhaps errno itself is undefined (and, by implication,
> unreadable)".  Ben seems to have taken it this way.  Of course,
> errno is defined in errno.h as a modifiable lvalue that has a type
> int.  The success or failure of a library function has no bearing on
> whether or not errno itself is defined.

> Perhaps Ben's problem was with the use of the term "undefined."  

Yes.

Quote:
> I see at least one other response on this thread saying that this
> term is used with a different meaning than it ought to have when
> used in the ANSI C context.  However, checking my copy of the
> standard, I see no definition there for the term "undefined."  There
> is a definition for the term "undefined behavior," but not for the
> term "undefined."

When I said ``undefined value'' in this context, I meant ``a value
which, when used, produces undefined behavior.''  (An example of this
sort of thing is the value of an uninitialized automatic variable in
C89.)  I assumed that was what Sosisor

at cross purposes.  In retrospect, another wording would have been
better.

--
"I ran it on my DeathStation 9000 and demons flew out of my nose." --Kaz



Thu, 04 Apr 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. fstream and files with unicode file names

2. bug in fstream? (works with fstream.h)

3. fstream ./. fstream.h

4. fatal: libXaw.so.5: can't open file: errno=2

5. ld.so.1: sh: fatal: libw.so.1: can't open file: errno=24

6. Errno 24 - Too many open files

7. How to read write file with std fstream

8. Place end of file in fstream

9. error opening fstream on second file

10. Trying to open file with fstream

11. header files: iostream, fstream,string

12. fstream cannot handle 46+ concurrently open files

 

 
Powered by phpBB® Forum Software