Error reason for fstream failures

S

Steve Keller

How can I get the reason for a file operation failure, say after
fstream::eek:pen() or fstream::read()?

In C, after fopen(), fread(), etc. you can check errno or get a
message using perror() and strerror().

I cannot find any equivalent for C++ fstreams.
 
M

Melzzzzz

How can I get the reason for a file operation failure, say after
fstream::eek:pen() or fstream::read()?

In C, after fopen(), fread(), etc. you can check errno or get a message
using perror() and strerror().

I cannot find any equivalent for C++ fstreams.

This is why my college said that streams are great evil ;)
On Linux one can still chek errno after stream failure.
 
N

Nobody

How can I get the reason for a file operation failure, say after
fstream::eek:pen() or fstream::read()?

In C, after fopen(), fread(), etc. you can check errno or get a message
using perror() and strerror().

Nothing in the C standards require fopen() or fread() to set errno on
failure. More generally, the standards only define the values EDOM, EILSEQ
and ERANGE, so anything else is implementation-dependent.
I cannot find any equivalent for C++ fstreams.

fstream::eek:pen() calls fstream::rdbuf()->open(), which operates "as if by
calling std::fopen()" (27.9.1.4[filebuf.members]p2).

In C++11, <cerrno> is aligned with POSIX rather than C, so EEXIST etc are
defined. However, fopen() is defined by reference to the C standard,
which doesn't specify its behaviour on failure with respect to errno (see
above). And POSIX doesn't mention C++.

So I guess it all depends upon what you want to read into "as if by
std::fopen()".
 
M

Marcel Müller

This is why my college said that streams are great evil ;)

Full ACK. I know about C++ streams about 20 years. And in all these 20
years of C++ programming I did not find even one use case where fstreams
fit the need best. I tested them from time to time, but it never made it
to production code.
Either I ended up with the C style I/O API (fopen...) or I directly
called the platform specific operating system functions. Mostly I took
the first choice. Only if the code is bound to one platform anyway I
used the OS APIs.
On Linux one can still chek errno after stream failure.

Yes, but the code is no longer portable.


Marcel.
 
J

Jorgen Grahn

Full ACK. I know about C++ streams about 20 years. And in all these 20
years of C++ programming I did not find even one use case where fstreams
fit the need best. I tested them from time to time, but it never made it
to production code.

What was wrong with it, all those times? Reading a line of text into
a std::string is one simple and very useful thing. Being able to print
a user-defined type is another.

/Jorgen
 
K

Kalle Olavi Niemitalo

Steve Keller said:
How can I get the reason for a file operation failure, say after
fstream::eek:pen() or fstream::read()?

In C++11, if you enable exceptions for the stream, then you can
get an error code from std::ios_base::failure, which is derived
from std::system_error.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

fstream Buffers 26
eof error using '>>' in fstream 2
fstream::open 5
std::fstream 7
Test if a std::fstream is open 1
error 28
fstream and number conversions 2
fstream lock question 1

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top