std::fstream

J

John Ratliff

Is this a legitimate (standards compliant) way to check for errors in
file I/O.

#include <fstream>

fstream output("file.ext", std::ios_base::eek:ut, std::ios_base::binary);

if (output) {
// we're good to write to file.ext in binary mode

output.close();
}

I thought I saw a C++ book say this was good, but I can't find a
reference on the internet and I don't know where my C++ book is right now.

I've seen this:

if (!output) {
// file wasn't opened
}

Also, Is binary default or do I need to specify it explicitly when I
open the fstream?

Thanks,

--John Ratliff
 
V

Victor Bazarov

John said:
Is this a legitimate (standards compliant) way to check for errors in
file I/O.

#include <fstream>

fstream output("file.ext", std::ios_base::eek:ut, std::ios_base::binary);

if (output) {
// we're good to write to file.ext in binary mode

output.close();
}

I thought I saw a C++ book say this was good, but I can't find a
reference on the internet and I don't know where my C++ book is right now.

I've seen this:

if (!output) {
// file wasn't opened
}

Also, Is binary default or do I need to specify it explicitly when I
open the fstream?

Every stream object allows you to call 'good()', 'fail()', and 'eof()',
which are considered enough to check for error conditions. More in your
book when you find it.

V
 
J

John Ratliff

Victor said:
Every stream object allows you to call 'good()', 'fail()', and 'eof()',
which are considered enough to check for error conditions. More in your
book when you find it.

My book is packed in a box until I move, so that won't be for awhile.

What exactly does if (output) mean here?

I know !output calls fail.

--John Ratliff
 
M

mlimber

John said:
Is this a legitimate (standards compliant) way to check for errors in
file I/O.

#include <fstream>

fstream output("file.ext", std::ios_base::eek:ut, std::ios_base::binary);

std::fstream output("file.ext",
std::ios_base::eek:ut | std::ios_base::binary);
if (output) {
// we're good to write to file.ext in binary mode

output.close();
}

I thought I saw a C++ book say this was good, but I can't find a
reference on the internet and I don't know where my C++ book is right now.

That'll make sure the file is opened, but other errors can occur along
the way. Also note that close() is called automatically by the
destructor if that's convenient for you. Try this reference:

http://www.cplusplus.com/ref/iostream/fstream/

The if statement's condition implicitly invokes io_base::eek:perator
void*() const, which basically returns 0 if fail() would return true or
non-zero if it wouldn't.

[snip]
Also, Is binary default or do I need to specify it explicitly when I
open the fstream?

It is not the default; text is.

Cheers! --M
 
J

John Ratliff

mlimber said:
std::fstream output("file.ext",
std::ios_base::eek:ut | std::ios_base::binary);

Heh heh. Yeah, forgot the std:: qualifier.
That'll make sure the file is opened, but other errors can occur along
the way. Also note that close() is called automatically by the
destructor if that's convenient for you. Try this reference:

http://www.cplusplus.com/ref/iostream/fstream/

The if statement's condition implicitly invokes io_base::eek:perator
void*() const, which basically returns 0 if fail() would return true or
non-zero if it wouldn't.

I thought it might be that, but I wasn't sure how it would do that
without an explicit cast.
[snip]
Also, Is binary default or do I need to specify it explicitly when I
open the fstream?


It is not the default; text is.

Thanks,

--John Ratliff
 
M

mlimber

John Ratliff wrote:
[snip]
Heh heh. Yeah, forgot the std:: qualifier.

And the | operator. :)
I thought it might be that, but I wasn't sure how it would do that
without an explicit cast.

That's a valid conversion (thanks to the conversion operator io_base
defines) and the best the compiler can find, so it takes it. (BTW, I
was looking at the code for STL-port, and it has a comment that says
the conversion operator is actually required in std::basic_ios, not
std::ios_base. STL-port defines it in the latter to eliminate
unnecessary code duplication from template instantiation.)

Cheers! --M
 
V

Victor Bazarov

John said:
My book is packed in a box until I move, so that won't be for awhile.

What exactly does if (output) mean here?

I know !output calls fail.

std::basic_ios has a conversion to 'void*' defined. That means you can
use it when a logical expression is expected. The pointer it returns is
non-null if the state is '!fail()'.

if (output)

is the same as

if (output.operator void*() != NULL)

or, semantically is the same as

if (!output.fail())

V
 
M

Maxim Yegorushkin

John said:
My book is packed in a box until I move, so that won't be for awhile.

What exactly does if (output) mean here?

I see you haven't packed your PC, so why don't you just look in
sources?

GNU ISO C++ Library:

template<typename _CharT, typename _Traits>
class basic_ios : public ios_base
{
// ...

//@{
/**
* @brief The quick-and-easy status check.
*
* This allows you to write constructs such as
* "if (!a_stream) ..." and "while (a_stream) ..."
*/
operator void*() const
{ return this->fail() ? 0 : const_cast<basic_ios*>(this); }

bool
operator!() const
{ return this->fail(); }
//@}
// ...
};
 

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

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top