There are two ways to create a file stream9: you can create an empty file stream, open a file, and connect it to the stream later on; or you can open the file and connect it to a stream at construction time. These two procedures are demonstrated in the two following examples, respectively:
ifstream file; //1 ...; file.open(argv[1]); //2 if (!file) // error: unable to open file for input
or:
ifstream source("src.cpp"); //3 if (!source) // error: unable to open src.cpp for input
//1 | A file stream is created that is not connected to any file. Any operation on the file stream fails. |
//2 | Here a file is opened and connected to the file stream. If the file cannot be opened, ios_base::failbit is set; otherwise, the file stream is now ready for use. |
//3 | Here the file is both opened and connected to the stream. |
Generally you can check whether the attempt to open a file was successful by examining the stream state afterwards; failbit is set in case of failure.
There is also a function called is_open() that indicates whether a file stream is connected to an open file. This function does not mean that a previous call to open() was successful. To understand the subtle difference, consider the case of a file stream that is already connected to a file. Any subsequent call to open() fails, but is_open() still returns TRUE, as shown in the following code:
void main(int argc, char* argv[]) { if (argc > 2) { ofstream fil; //1 fil.open(argv[1]); // ... fil.open(argv[2]); //2 if (fil.fail()) //3 { // open failed } if (fil.is_open()) //4 { // connected to an open file } } }
//1 | Open a file and connect the file stream to it. |
//2 | Any subsequent open on this stream fails. |
//3 | Hence the failbit is set. |
//4 | However, is_open() still returns TRUE, because the file stream still is connected to an open file. |
In the example above, it would be advisable to close the file stream before you try to connect it to another file. This is done implicitly by the file streams destructor in the following code:
void main(int argc, char* argv[]) { if (argc > 2) { ofstream fil; fil.open(argv[1]); // ... } //1 { ofstream fil; fil.open(argv[2]); // ... } }
//1 | Here the file stream fil goes out of scope and the file it is connected to is closed automatically. |
You can explicitly close the connected file. The file stream is then empty, until it is reconnected to another file:
ifstream f; //1 for (int i=1; i<argc; ++i) { f.open(argv[i]); //2 if (f) //3 { process(f); //4 f.close(); //5 } else cerr << "file " << argv[i] << " cannot be opened.\n"; }
//1 | An empty file stream is created. |
//2 | A file is opened and connected to the file stream. |
//3 | Here we check whether the file was successfully opened. If the file could not be opened, the failbit would be set. |
//4 | Now the file stream is usable, and the file's content can be read and processed. |
//5 | Close the file again. The file stream is empty again. |
OEM Edition, ©Copyright 1999, Rogue Wave Software, Inc.
Contact Rogue Wave about documentation or support issues.