Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Error reason for fstream failures

Reply
Thread Tools

Error reason for fstream failures

 
 
Steve Keller
Guest
Posts: n/a
 
      02-05-2013
How can I get the reason for a file operation failure, say after
fstream: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.
 
Reply With Quote
 
 
 
 
Melzzzzz
Guest
Posts: n/a
 
      02-05-2013
On Tue, 05 Feb 2013 15:11:28 +0100, Steve Keller wrote:

> How can I get the reason for a file operation failure, say after
> fstream: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.
 
Reply With Quote
 
 
 
 
Nobody
Guest
Posts: n/a
 
      02-06-2013
On Tue, 05 Feb 2013 15:11:28 +0100, Steve Keller wrote:

> How can I get the reason for a file operation failure, say after
> fstream: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: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()".

 
Reply With Quote
 
Marcel Müller
Guest
Posts: n/a
 
      02-06-2013
On 05.02.13 15.12, Melzzzzz wrote:
> 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.
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      02-06-2013
On Wed, 2013-02-06, Marcel Mller wrote:
> On 05.02.13 15.12, Melzzzzz wrote:
>> 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.


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

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Kalle Olavi Niemitalo
Guest
Posts: n/a
 
      02-06-2013
Steve Keller <(E-Mail Removed)> writes:

> How can I get the reason for a file operation failure, say after
> fstream: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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
eof error using '>>' in fstream David C++ 2 02-11-2010 07:35 PM
fstream error on SUN ratzeel C++ 3 11-09-2005 11:26 AM
compile error, fstream.h, g++ version Pasacco C++ 1 04-01-2005 04:19 PM
what is different between <fstream.h> and <fstream>MS VC++ Armando C++ 6 01-29-2004 09:01 AM
Q: how to get the error from fstream << zhou C++ 1 07-23-2003 09:11 PM



Advertisments