cin cout ifstream ofstream

File I/O IO streams were originally developed by AT&T. Since then minor changes have been made, but the functionality is essentially the same. Data co...
Author: Arleen Fletcher
7 downloads 1 Views 26KB Size
File I/O IO streams were originally developed by AT&T. Since then minor changes have been made, but the functionality is essentially the same. Data coming in or going out of the computer can be viewed as an infinite stream of bytes, and the stream library provides controlled access to this data. Code examples in this document assume the std namespace.

cin

cout ostream

istream keyboard

monitor ofstream

ifstream

The above diagram illustrates streams cin and cout, the console I/O streams. You can use the same stream mechanism, with minor adjustments, to do I/O to files or C++ strings. The following code illustrates file input based on the ifstream. // open file ifstream ifile; ifile.open("file.dat", ios::in); // ifile now behaves just as cin ifile >> x; // release resources ifile.close();

The relationship between the I/O stream classes are shown below. Class ios_base contains data formatting information and flags. Class ios, derived from ios_base, adds buffering and character type information. Classes istream and ostream are the main input/output classes. Use the derived fstream classes for file stream I/O, and the derived sstream classes for string stream I/O.



ios_base

istream





ifstream istringstream

ios

iostream

fstream stringstream

ofstream ostream

ostringstream



Include file (not shown above) contains the following definitions: istream ostream ostream ostream

cin; cout; cerr; clog;

// // // //

standard input (stdin) buffered to console (stdout) unbuffered to console (stderr) buffered to console

Files often include dependent files. For example, includes , so it's not necessary to include all files. One of the following should suffice:

// cin/cout // file stream operations // string stream operations

In addition, include file for formatting for functions that have parameters.

Examples To open a file call the open member function. Alternatively, you can utilize the stream constructor that assumes the same arguments. For example, the following code segments are identical in function: ifstream ifile; ifile.open("file.dat", ios::in); ifstream ifile("file.dat", ios::in);

The first parameter is the filename, and can be a literal or null-terminated C-style character string. char *fname = "file.dat"; ifstream ifile(fname, ios::in);

If you wish to use a C++ string, then you must invoke the c_str member function that returns a Cstyle string. string fname = "file.dat"; ifstream ifile(fname.c_str(), ios::in);

The second parameter designates access mode and can be one of the following: C r w a r+ w+ a+

C++ in out out|app in|out in|out|trunc in|out|app

Meaning file must exist, read only create/overwrite, write only create/append, write only file must exist, read-write create/overwrite, read-write create/append, read-write

Normally special characters are translated by the I/O routines. For example, a tab character ("\t", or 0x09) will appear as whitespace if displayed on the screen. To suppress translation, specify ios::binary.

You can check file stream return codes just as you check cin/cout: #include ifstream ifile("file.dat", ios::in); if (!ifile) { // open failed, errno has error // implemented for C, may or may not be implemented for C++ cerr

Suggest Documents