Files and Streams in C++
Files and Streams in C++
Now how to read and write from a file. This requires another standard C++ library
called fstream, which defines three new data types −
1
ofstream
This data type represents the output file stream and is used to create files and to write
information to files.
2
ifstream
This data type represents the input file stream and is used to read information from files.
3
fstream
This data type represents the file stream generally, and has the capabilities of both
ofstream and ifstream which means it can create files, write information to files, and read
information from files.
To perform file processing in C++, header files <iostream> and <fstream> must be included in
your C++ source file.
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.
1
ios::app
Append mode. All output to that file to be appended to the end.
2
ios::ate
Open a file for output and move the read/write control to the end of the file.
3
ios::in
Open a file for reading.
4
ios::out
Open a file for writing.
5
ios::trunc
If the file already exists, its contents will be truncated before opening the file.
It is possible to 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 );
When a C++ program terminates it automatically flushes all the streams, release all the
allocated memory and close all the opened files. But it is always a good practice that a
programmer should close all the opened files before program termination.
Following is the standard syntax for close() function, which is a member of fstream, ifstream,
and ofstream objects.
void close();
Writing to a File
While doing C++ programming, you write information to a file from your program using the
stream insertion operator (<<) just as you use that operator to output information to the screen.
The only difference is that you use an ofstream or fstream object instead of the cout object.
#include <fstream>
#include <iostream>
using namespace std;
int main ()
{
char data[100];
// again read the data from the file and display it.
infile >> data;
return 0;
}
When the above code is compiled and executed, it produces the following sample input and
output −
Output:
Writing to the file
Enter your name: Zara
Enter your age: 9
Reading from the file
Zara
9
Above examples make use of additional functions from cin object, like getline() function to read
the line from outside and ignore() function to ignore the extra characters left by previous read
statement.
The argument to seekg and seekp normally is a long integer. A second argument can be
specified to indicate the seek direction. The seek direction can be ios::beg (the default) for
positioning relative to the beginning of a stream, ios::cur for positioning relative to the current
position in a stream or ios::end for positioning relative to the end of a stream.
The file-position pointer is an integer value that specifies the location in the file as a
number of bytes from the file's starting location. Some examples of positioning the "get" file-
position pointer are −