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 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
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
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
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
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
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
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(); }
//@}
// ...
}; This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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();
|
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...
|
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;
|
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...
| |
by: neowillis |
last post by:
code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
|
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...
|
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="";
|
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: ...
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
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 ...
| |
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...
| |