473,509 Members | 2,528 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::fstream

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

#include <fstream>

fstream output("file.ext", std::ios_base::out, 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
Sep 1 '05 #1
7 10028
John Ratliff wrote:
Is this a legitimate (standards compliant) way to check for errors in
file I/O.

#include <fstream>

fstream output("file.ext", std::ios_base::out, 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
Sep 1 '05 #2
Victor Bazarov wrote:
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
Sep 1 '05 #3
John Ratliff wrote:
Is this a legitimate (standards compliant) way to check for errors in
file I/O.

#include <fstream>

fstream output("file.ext", std::ios_base::out, std::ios_base::binary);
std::fstream output("file.ext",
std::ios_base::out | 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::operator
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

Sep 1 '05 #4
mlimber wrote:
John Ratliff wrote:
Is this a legitimate (standards compliant) way to check for errors in
file I/O.

#include <fstream>

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

std::fstream output("file.ext",
std::ios_base::out | std::ios_base::binary);


Heh heh. Yeah, forgot the std:: qualifier.
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::operator
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
Sep 1 '05 #5
John Ratliff wrote:
[snip]
fstream output("file.ext", std::ios_base::out, std::ios_base::binary);


std::fstream output("file.ext",
std::ios_base::out | std::ios_base::binary);


Heh heh. Yeah, forgot the std:: qualifier.


And the | operator. :-)
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::operator
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.


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

Sep 1 '05 #6
John Ratliff wrote:
Victor Bazarov wrote:
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.


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
Sep 1 '05 #7

John Ratliff wrote:
Victor Bazarov wrote:
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 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(); }
//@}
// ...
};

Sep 2 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
15282
by: Maya | last post by:
Apart from being a pointer, what would be the benefit of using 'std::filebuf' than using the std::fstream? As far as I can see, I would use the same methods in 'filebuf' that I would when using...
6
3203
by: David Briggs | last post by:
I am using MS VC++ 6.0 with MFC I have a simple class: #include <fstream.h> class Data { public: CString WriteStr(); Data();
9
5268
by: Someonekicked | last post by:
In my program, I need to open multiple files, and I wont know till after the program execution how many of them (user will enter that value). So I am using a vector of fstream. I am using fstream...
6
17102
by: wiso | last post by:
My problem is this (from: http://www.cplusplus.com/ref/iostream/fstream/open.html) #include <fstream> using namespace std; int main() { fstream f;
2
4743
by: jjcp | last post by:
I would like to say thanks in advance for insight anyone can shed on this for me. Long story short from time to time I need to us C++ to take a list of file and make an index out of them in...
5
4146
by: neowillis | last post by:
code: #include <iostream> #include <fstream> #include <string> using namespace std; int main() {
7
9931
by: Soneji | last post by:
*sigh* Ok, I have two questions. First, my setup: Win-doze XP-SP2; & Dev-C++ 4.9.9.2; I recently started trying to use 'fstream' to work my input/output, and I noticed that using the...
6
3719
by: Gaijinco | last post by:
Should this do something? #include <fstream> #include <string> int main() { std::fstream filestr ("test.txt", std::fstream::in | std::fstream::out); std::string s="";
6
1953
by: canilao | last post by:
Hi All, So I ran into an issue with using the ! operator with fstream: // Make sure the file is ready to be read before we move on. 1: fstream fileStream; 2: ...
0
7233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7135
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7342
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7410
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7067
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
440
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.