11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 24: Using Stream I/O 317ostream object as follows. (In binary mode, newlines are not converted tocarriage returns and line feeds on output, nor are carriage returns and linefeeds converted back to newlines on input.)void fn(){//open the binary file BINFILE for writing; if it//exists, append to end of whatever’s already there}ofstream bfile(“BINFILE”, ios::binary | ios::ate);//...continue on as before...The stream objects maintain state information about the I/O process. Themember function bad() returns a TRUE if something “bad” happens. That nebulousterm means that the file couldn’t be opened, some internal object wasmessed up, or things are just generally hosed. A lesser error fail() indicatesthat either something bad() happened or the last read failed — for example, ifyou try to read an int and all the program can find is a character that rates afail() but not a bad(). The member function good() returns TRUE if bothbad() and fail() are FALSE. The member function clear() zeros out theerror flag to give you another chance. The following program adds basic errorchecking to the StreamOutput program:// StreamOutputWithErrorChecking - simple output to a file#include #include using namespace std;int main(int nNumberofArgs, char* pszArgs[]){const static char fileName[] = “MyName.txt”;ofstream my(fileName);if (my.bad()) //if the open didn’t work...}{cerr

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!