Opening a File
A file must be opened before you can read from it or
write to it. Either ofstream or fstream object may
be used to open a file for writing. And ifstream
object is used to open a file for reading purpose only.
Following is the standard syntax for open() function,
which is a member of fstream, ifstream, and ofstream objects.
void open(const char *filename, ios::openmode mode);
Mode Flag Description
ios::app Append mode. All output to that
file to be appended to the end.
ios::ate Open a file for output and move the read/write
control to the end of the file.
ios::in Open a file for reading.
ios::out Open a file for writing.
ios::trunc If the file already exists, its contents
will be truncated before opening the file.
You can combine two or more of these values by ORing them together.
For example if you want to open a file in write mode and want to
truncate it in case that already exists, following will be the syntax:
ofstream outfile;
outfile.open("file.dat", ios::out | ios::trunc );
Similar way, you can open a file for reading and writing purpose as follows:
fstream afile;
afile.open("file.dat", ios::out | ios::in );
No comments:
Post a Comment