#include <fstream.h>void open(const char *filename, int mode, int access=filebuf::openprot);
The open( ) function is a member of fstream, ifstream, and ofstream.
A file is associated with a stream by using the open( ) function. Here, filename is the name of the file, which may include a path specifier. The value of mode determines how the file is opened. It must be one (or more) of these values:
ios::app
ios::ate
ios::binary
ios::in
ios::nocreate
ios::noreplace
ios::out
ios::trunc
You can combine two or more of these values by ORing them together.
| Programming Tip |
To read from or write to a text file, you simply use the << and >> operators with the stream you opened. For example, the following old-style program writes an integer, a floating-point value, and a string to a file called “test” and then reads them back: // This is an old-style program.
#include <iostream.h>
#include <fstream.h>
int main()
{
ofstream out("test");
if(!out) {
cout << "Cannot open file.\n";
return 1;
}
// output data
out << 10 << " " << 123.23 << "\n";
out << "This is a short text file.\n";
out.close();
// now, read it back
char ch;
int i;
float f;
char str[80];
ifstream in("test");
if(!in) {
cout << "Cannot open file.\n";
return 1;
}
in >> i;
in >> f;
in >> ch;
in >> str;
cout << "Here is the data: ";
cout << i << " " << f << " " << ch << "\n";
cout << str;
in.close();
return 0;
}
When reading text files using the >> operator, keep in mind that certain character translations occur. For example, whitespace characters are omitted. If you want to prevent any character translations, you must open the file for binary I/O and use the binary I/O functions. |
Including ios::app causes all output to that file to be appended to the end. This value can only be used with files capable of output. Including ios::ate causes a seek to the end of the file to occur when the file is opened. Although ios::ate causes a seek to the end of file, I/O operations can still occur anywhere within the file.
The ios::binary value causes the file to be opened for binary I/O operations. By default, files are opened in text mode.
The ios::in value specifies that the file is capable of input. The ios::out value specifies that the file is capable of output. However, creating a stream using ifstream implies input, and creating a stream using ofstream implies output, so in these cases it is unnecessary to supply these values.
The ios::trunc value causes the contents of a preexisting file by the same name to be destroyed and the file is truncated to zero length.
Including ios::nocreate causes the open( ) function to fail if the file does not already exist. The ios::noreplace value causes the open( ) function to fail if the file already exists and ios::ate or ios::app is not also specified.
The value of access determines how the file can be accessed. Its default value is filebuf::openprot (filebuf is a base class of the file classes), which means a normal file. Check your compiler's documentation for other legal values of access.
When opening a file, both mode and access will default. When opening an input file, mode will default to ios::in. When opening an output file, mode will default to ios::out. In either case, the default for access is a normal file. For example, this opens a file called “test” for output:
out.open("test"); // defaults to output and normal file
To open a stream for input and output, you must usually specify both the ios::in and the ios::out mode values, as shown here:
mystream.open("test", ios::in | ios::out);
For many compilers, no default value for mode is supplied when opening read/write files.
In all cases, if open( ) fails, the stream will be zero. Therefore, before using a file, you should test to make sure that the open operation succeeded.
Related functions are close( ), fstream( ), ifstream( ), and ofstream( ).