Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   Error reason for fstream failures (http://www.velocityreviews.com/forums/t957301-error-reason-for-fstream-failures.html)

Steve Keller 02-05-2013 02:11 PM

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

Melzzzzz 02-05-2013 02:12 PM

Re: Error reason for fstream failures
 
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::open() 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.

Nobody 02-06-2013 03:15 AM

Re: Error reason for fstream failures
 
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::open() 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::open() 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()".


Marcel Müller 02-06-2013 06:54 AM

Re: Error reason for fstream failures
 
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.

Jorgen Grahn 02-06-2013 08:08 PM

Re: Error reason for fstream failures
 
On Wed, 2013-02-06, Marcel Müller 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 .

Kalle Olavi Niemitalo 02-06-2013 08:53 PM

Re: Error reason for fstream failures
 
Steve Keller <keller@no.invalid> writes:

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


All times are GMT. The time now is 04:22 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.