C++ File Handing
C++ File Handing
1
File Names
All files are assigned a name that is used for identification
purposes by the operating system and the user.
Focus on Software Engineering:
The Process of Using a File
• Using a file in a program is a simple three-step process
• Information is then saved (write)to the file, read from the file, or
both.
• When the program is finished using the file, the file must be
closed.
C++ Files and Streams
5
open a file
The first operation generally performed on an object of one of these classes is to
associate it to a real file. This procedure is known as to open a file.
OR
Ofstream outClientFile;
outClientFile.open(“clients.dat”, ios:out) 6
File Open Modes
ios::in Open for input operations.
ios::out Open for output operations.
ios::binary Open in binary mode.
Set the initial position at the end of the file.
ios::ate
If this flag is not set, the initial position is the beginning of the file.
All output operations are performed at the end of the file,
ios::app
appending the content to the current content of the file.
If the file is opened for output operations and it already existed,
ios::trunc
its previous content is deleted and replaced by the new one.
7
Testing for Open Errors
is_open()
• To check if a file stream was successful opening a file
• True in the case that indeed the stream object is associated with an open file, or false
otherwise:
if ( myfile.is_open() )
{ /* ok, proceed with output */ }
8
Testing for Open Errors
dataFile.open(“cust.dat”, ios::in);
if (!dataFile)
{
cout << “Error opening file.\n”;
}
9
Another way to Test for Open Errors
dataFile.open(“cust.dat”, ios::in);
if ( dataFile.fail() )
{
cout << “Error opening file.\n”;
}
10
How to close a file in C++?
The file is closed implicitly when a destructor for the corresponding object
is called
OR
by using member function close:
myfile.close();
Once this member function is called, the stream object can be
re-used to open another file, and the file is available again to
be opened by other processes.
11
Text file
streams are those where the ios::binary flag is not included in their
opening mode.
These files are designed to store text and thus all values that are input
or output from/to them can suffer some formatting transformations,
which do not necessarily correspond to their literal binary value.
WRITING ON A TEXT FILE
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile ("example.txt");
if (myfile.is_open())
{
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
READING A TEXT FILE
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << '\n';
}
myfile.close();
}
return 0;
}
• This last example reads a text file and prints out its
content on the screen.
True
False
getline (myfile,line);
fail()
Returns true in the same cases as bad(), but also in the case that a
format error happens, like when an alphabetical character is
extracted when we are trying to read an integer number.
eof()
Returns true if a file open for reading has reached the end.
good()
It is the most generic state flag: it returns false in the same cases in
which calling any of the previous functions would return true. Note
that good and bad are not exact opposites (good checks more
state flags at once).
clear()
can be used to reset the state flags.