Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

Checking the file I/O status in Open C++

From Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

The article is believed to be still valid for the original topic scope.

Article Metadata
Tested with
Devices(s): Nokia 6220 Classic
Platform(s): S60 3rd Edition, FP2
Keywords: fstream, fstream::rdstate(), fstream::good(), fstream::bad(), fstream::eof(), fstream::fail()
Created: aknyman (24 Sep 2008)
Last edited: lpvalente (23 Sep 2014)



This code snippet shows how to get information about file I/O status in Open C++. The member function rdstate() returns the current internal error state flags of the stream (goodbit, eofbit, failbit, badbit). More than one error state could be set at the same time and it is possible to check for a specific state with the & operator and a specific bitmask. The member function rdstate() returns value goodbit if there are no errors. The other way to check the file I/O status is to use the member functions good(), eof(), fail(), and bad(). The member function clear() can be used to reset the state flags.

Note: In order to use this code, you need to install the Open C/C++ plug-in.

This snippet can be self-signed.

MMP file

The following libraries are required:

LIBRARY libstdcpp.lib
LIBRARY libc.lib
LIBRARY euser.lib

Source file

#include <fstream>  //fstream
#include <string> //string, getline()
#include <iostream> //cout
using namespace std;
void check_stream_state1(const fstream &s)
int state = s.rdstate();
/* State flags:
goodbit = 0x00, //file has been opened without problems
badbit = 0x01, //e.g. writing to a file that is not open / no space left
eofbit = 0x02, //file opened for reading has reached the end
failbit = 0x04, //e.g. format error: alphabetical character is extracted
//from the stream but integer is expected

//goodbit is a constant with the value 0
if (!state)
cout << "goodbit - no error occurred" << endl;
if (state & ios::badbit)
cout << "badbit - a fatal I/O error has occurred" << endl;
if (state & ios::eofbit)
cout << "eofbit - end of file is encountered" << endl;
if (state & ios::failbit)
cout << "failbit - a nonfatal I/O error has occurred" << endl;
void check_stream_state2(const fstream &s)
//true if no error indicators are set
cout << "good() returned true" << endl;
//true if a stream is marked as unusable (badbit)
if (s.bad())
cout << "bad() returned true" << endl;
//true if end of file is reached on the stream
if (s.eof())
cout << "eof() returned true" << endl;
//true if some operation failed (failbit) or
//the stream is marked as unusable (badbit)
cout << "fail() returned true" << endl;
int main()
fstream output("stream.txt", ios::out);
//using member function good()
//write text to the file
output << "Text To File" << endl;
//check that goodbit is set
//close the output stream
//try to write text to a file that is not open
output << "Some More Text" << endl;
//check that badbit and failbit are set
fstream input("stream.txt", ios::in);
string text;
//using member function eof()
getline (input, text);
cout << text << endl;
//reset/set state flag to be goodbit
//try to write to input-only stream
input << "Some More Text" << endl;
//check that badbit and failbit are set
//reset/set state flag to be goodbit
//move read pointer back to the beginning of the file
int number = 0;
//try to extract integer from the stream
input >> number;
//check that failbit is set
//close the input stream
return 0;


The example application has created the file stream.txt containing one line of text. check_stream_state() functions have displayed state flag information about file I/O statuses as standard output.

See also

This page was last modified on 23 September 2014, at 19:49.
119 page views in the last 30 days.