File Management
File Management
5. File Management
File handling is an important part of all programs. Most of the applications will have their
own features to save some data to the local disk and read data from the disk again. Files
which are on the secondary storage device are called physical files. In order to process file
through program, logical file must be created on the RAM. This logical file is nothing but
an object having file data type. As an object there should be a variable identifier that points
to it. This variable is called file variable and some times also called file handler. C++ File
I/O classes simplify such file read/write operations for the programmer by providing easier
to use classes.
Streams
The C++ file system is designed to work with a wide variety of devices, including
terminals, disk drives, and tape drives. Even though each device is very different, the C++
file system transforms each into a logical device called stream. There are two types of
streams: text and binary.
a. Text Streams
A text stream is a sequence of characters. In a text stream, certain character translations
may occur as required by the host environment. For example a new line may be converted
to a carriage return/linefeed pair. There may not be a one-to-one relationship between the
characters that are written (or read) and those on the external device. Because of possible
transformations, the number of characters written (or read) may not be the same as those
on the external device.
b. Binary streams
A binary stream is a sequence of bytes with a one-to-one correspondence to those in the
external device i.e., no character translations occur. The number of bytes written (or read)
-1-
is the same as the number on the external device. However, an implementation-defined
number of null bytes may be appended to a binary stream. These null bytes might be used
to pad the information so that it fills a sector on a disk, for example.
Files
In C++, a file can be anything from a disk file to a terminal or printer. You associate a
stream with a specific file by performing an open operation. Once a file is open,
information can be exchanged between it and a program. All streams are the same but all
files are not. If the file can support position requests, opening that file also initializes the
file position indicator to the start of the file. As each character is read from or written to
the file, the position indicator is incremented. You disassociate a file from a specific stream
with a close operation. If you close a file opened for output, then contents, if any, of its
associated stream are written to the external device. -- this process is referred to as flushing
the stream. All files are closed automatically when the program terminates normally. Files
are not closed when a program terminates abnormally. Each stream that is associated with
a file has a file control structure of type FILE. This structure FILE is defined in the header
stdio.h.
The File Pointer
A file pointer is a pointer to information that defines various things about the file, including
its name, status, and the current t position of the file. In essence, the file pointer identifies
a specific disk file and is used by the associated stream to direct the operation of the I/O
functions. A file pointer is a pointer variable of type FILE.
FILE * fp;
-2-
5.2 C++ File I/O Classes and Functions
To perform file I/O, the header file fstream.h is requied. fstream.h defines several classes,
including ifstream, ofstream, and fstream. These classes are derived form istream and
ostream, repectively. istream and ostream are derived form ios.
Three file I/O classes are used for File Read/Write operations:
a. ifstream - Can be used for File read/input operations
b. ofstream - Can be used for File write/output operations
c. fstream - Can be used for both read/write c++ file I/O operations
These classes are derived directly or indirectly from the classes istream, and ostream. We
have already used objects whose types were these classes: cin is an object of class istream
and cout is an object of class ostream. Therefore, we have already been using classes that
are related to our file streams. And in fact, we can use our file streams the same way we
are already used to use cin and cout, with the only difference that we have to associate
these streams with physical files. Let's see an example:
Text and Binary Files
In file processing, files are generally classified into two as
Text file and
Binary file
Text file is a file in which its content is treated as a sequence of characters and can be
accessed sequentially. Where as binary file is a file in which its content is treated as record
sequence in a binary format. Binary format refers to the actual format the data is going to
be placed and processed in the memory which is directly related to its data type.
For example, the value int count 321 will be stored in three byte if it is written in text file
considering the digit sequence ‘3’, ‘2’, ‘1’. It will be stored in two byte if it is written in
binary file since int requires two byte to store any of its value. When you open the binary
file you will see the character equivalence of the two bytes.
321 in binary equals 0000 0001 0100 0001
The first byte holds the character with ASCII value equals to one and the second byte a
character with ASCII value equals 65 which is ‘A’. Then if you open the binary file you
will see these characters in place of 321.
-3-
5.3 Text File processing
File processing involves the following major steps
1. Declaring file variable identifier
2. Opening the file
3. Processing the file
4. Closing the file when process is completed.
ios::ate Open a file for output and move to the end of the file (normally used to append
data to a file). Data can be written anywhere in the file.
-4-
ios::out Open a file for output
ios:trunc Discard the file's content if it exists (this is also the default action ios::out)
You can combine two or more of these values by using them together.
ofstream out ;
out.open ( "test", ios::out); // correct statement
ofstream out1 ;
out.open ( " test"); // the default value of mode is ios::out –
// correct statment
To open a stream for input and output, you must specify both the ios::in and the ios::out
mode values. (Noe default value for mode is supplied in this case.)
fstream myStream;
myStream.open ( "test", ios::in | ios::out );
If open ( ) fails, myStrream will be zero
if (myStream){
cout << "Cannot open a file.\n";
// handle error
}
Each one of the open() member functions of the classes ofstream, ifstream and fstream has
a default mode that is used if the file is opened without a second argument:
class default mode parameter
ofstream ios::out
ifstream ios::in
fstream ios::in | ios::out
For ifstream and ofstream classes, ios::in and ios::out are automatically and respectively
assumed, even if a mode that does not include them is passed as second argument to the
open() member function. The default value is only applied if the function is called without
specifying any value for the mode parameter. If the function is called with any value in that
parameter the default mode is overridden, not combined.
File streams opened in binary mode perform input and output operations independently of
any format considerations. Non-binary files are known as text files, and some translations
may occur due to formatting of some special characters (like newline and carriage return
characters).
Since the first task that is performed on a file stream object is generally to open a file, these
three classes include a constructor that automatically calls the open() member function and
has the exact same parameters as this member. Therefore, we could also have declared the
-5-
previous myfile object and conducted the same opening operation in our previous example
by writing:
ofstream myfile ("example.bin", ios::out | ios::app | ios::binary);
Combining object construction and stream opening in a single statement. Both forms to
open a file are valid and equivalent.
To check if a file stream was successful opening a file, you can do it by calling to member
is_open() with no arguments. This member function returns a bool value of 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 */ }
ifstream myStream ( "myfile" ); // open file for input
When we are finished with our input and output operations on a file we shall close it so
that its resources become available again. In order to do that we have to call the stream's
member function close(). This member function takes no parameters, and what it does is
to flush the associated buffers and close the file:
myfile.close();
Once this member function is called, the stream object can be used to open another file,
and the file is available again to be opened by other processes.
In case that an object is destructed while still associated with an open file, the destructor
automatically calls the member function close(). The close method takes no parameters and
returns no value.
-6-
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
This code creates a file called example.txt and inserts a sentence into it in the same way we
are used to do with cout, but using the file stream myfile instead.
Example 2: writing on a text file
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream myfile ("example.txt");
if (myfile.is_open())
{
myfile << "This is a line.\n";
myfile << "This is another line.\n";
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
Example 3: 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 (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}
return 0;
}
This last example reads a text file and prints out its content on the screen. Notice how we
have used a new member function, called eof() that returns true in the case that the end of
-7-
the file has been reached. We have created a while loop that finishes when indeed
myfile.eof() becomes true (i.e., the end of the file has been reached).
Checking state flags
In addition to eof(), which checks if the end of file has been reached, other member
functions exist to check the state of a stream (all of them return a bool value):
Function Description
bad() Returns true if a reading or writing operation fails. For example in the
case that we try to write to a file that is not open for writing or if the
device where we try to write has no space left.
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.
In order to reset the state flags checked by any of these member functions we have just
seen we can use the member function clear(), which takes no parameters.
get and put stream pointers
All I/O streams objects have, at least, one internal stream pointer:
ifstream, like istream, has a pointer known as the get pointer that points to the
element to be read in the next input operation.
ofstream, like ostream, has a pointer known as the put pointer that points to the
location where the next element has to be written.
Finally, fstream, inherits both, the get and the put pointers, from iostream (which is
itself derived from both istream and ostream).
These internal stream pointers that point to the reading or writing locations within a stream
can be manipulated using the following member functions:
tellg() and tellp()
These two member functions have no parameters and return a value of the member type
pos_type, which is an integer data type representing the current position of the get stream
pointer (in the case of tellg) or the put stream pointer (in the case of tellp).
-8-
seekg() and seekp()
These functions allow us to change the position of the get and put stream pointers. Both
functions are overloaded with two different prototypes. The first prototype is:
seekg ( position );
seekp ( position );
Using this prototype the stream pointer is changed to the absolute position position
(counting from the beginning of the file). The type for this parameter is the same as the one
returned by functions tellg and tellp: the member type pos_type, which is an integer value.
The other prototype for these functions is:
-9-
5.4 Binary File processing
In binary files, to input and output data with the extraction and insertion operators (<< and
>>) and functions like getline is not efficient, since we do not need to format any data, and
data may not use the separation codes used by text files to separate elements (like space,
newline, etc...).
File streams include two member functions specifically designed to input and output binary
data sequentially: write and read. The first one (write) is a member function of ostream
inherited by ofstream. And read is a member function of istream that is inherited by
ifstream. Objects of class fstream have both members. Their prototypes are:
Where memory_block is of type "pointer to char" (char*), and represents the address of an
array of bytes where the read data elements are stored or from where the data elements to
be written are taken. The size parameter is an integer value that specifies the number of
characters to be read or written from/to the memory block.
There are two ways to write and read binary data to and from a file.
get ( ) and put ( )
read ( ) and write ( )
If you will be performing binary operations on a file, be sure to open it using the ios::binary
mode specifier.
- 10 -
char in;
ifstream in ( "test", ios::in | ios::binary);
if (!in){
cout <<"Cannot open file";
return 1;
}
while (in) //inn will be 0 when eof is reached
{ in.get ( ch );
cout << ch;
}
When the end-of-file is reached, the stream associated with the file becomes zero.
ofstream out ( "chars", io::out | ios::binary);
for (int i= 0; i < 256; i++)
out.put ( (char ) i ) ; //write all characters to disk
out.close ( );
int gcount ( );
This get ( ) method reads characters into the array pointed to by the buf until either num
characters have been read, or the character specified by delim has been encountered. The
array pointed to by buf will be null terminated by get ( ). If the delimiter character is
encountered in the input stream, it is not extracted. Instead, it remains in the stream until
the next input operation.
- 11 -
a. int get ( )
It returns the next character from the stream. It returns EOF if the end of file is
encountered.
b. getline ( )
istream & getline ( char *buf, int num, char delim ='\n');
This method is virtually identical to the get ( buf, num, delim) version of get ( ). The
difference is getline ( ) reads and removes the delimiter from the input stream.
#include <iostream>
#include <fstream>
using namespace std;
ifstream::pos_type size;
char * memblock;
int main () {
ifstream file ("example.txt", ios::in|ios::binary|ios::ate);
if (file.is_open())
{
size = file.tellg();
memblock = new char [size];
file.seekg (0, ios::beg);
file.read (memblock, size);
file.close();
delete[] memblock;
}
else cout << "Unable to open file";
return 0;
}
In this example the entire file is read and stored in a memory block. Let's examine how this
is done:
First, the file is open with the ios::ate flag, which means that the get pointer will be
positioned at the end of the file. This way, when we call to member tellg(), we will directly
obtain the size of the file. Notice the type we have used to declare variable size:
ifstream::pos_type size;
ifstream::pos_type is a specific type used for buffer and file positioning and is the type
returned by file.tellg(). This type is defined as an integer type, therefore we can conduct on
it the same operations we conduct on any other integer value, and can safely be converted
- 12 -
to another integer type large enough to contain the size of the file. For a file with a size
under 2GB we could use int:
int size;
size = (int) file.tellg();
Once we have obtained the size of the file, we request the allocation of a memory block
large enough to hold the entire file:
Function Description
Detecting EOF It returns nonzero when the end of the file has been reached;
int eof ( ); otherwise it returns zero.
Reading and discarding characters Reads and discards characters until either num characters have
from the input stream. been nignored (1 by default ) or until the charcter specified by
istream & ignore (int num = 1, int delim is encounterdd (EOF by default). If the delimiting
delim = EOF); character is encountered, it is not removed from the input
stream.
Obtain the next character in the input One can obtain the next character in the input stream without
stream without removing it from that removing it from that stream by using peek ( ). It returns the
stream next character in the stream or EOF if the end of file is
int peek ( ); encountered.
istream & putback ( char c); One can return the last character read from a stream to that
stream using putback ( ).
- 13 -
Forcing data to be physically written to When the output is performed, data is not necessarily
the disk immediately written to the physical device linked to the
stream. Instead, information is stored in an internal buffer until
ostream & flush ( ); the buffer is full. Only then are the contents of that buffer
written to disk. However, you can force the information to be
physically written to the disk before the buffer is full by
calling flush ( ).
ios::beg
ios::cur
ios::end
The C++ I/O system manages two pointers associated with a file. One is the get pointer,
which specifies where in then file the next input operation will occur. The other is the put
pointer, which specifies where in the file the next output operation will occur. Each time
an input or output operation takes place the appropriate pointer is automatically
sequentially advanced. The seekg ( ) method moves the associated file's current get pointer
offset number of bytes from the specified origin, which must be one of three values. The
seekp ( ) method moves the associated file's current put pointer offset number of bytes from
the specified origin, which must be one of three values.
streampos tellg ( );
streampows tellp ( );
Here, streampos is a type defined in iostream.h that is capable of holding the largest value
that either function can return.
- 14 -
5.6 I/O Status
The C++ I/O system maintains status information about the outcome of each I/O operation.
The current state of the I/O system is held in an integer, in which the following flags are
encoded.
These flags are enumerated inside ios. Also defined in ios is goodbit, which has the value
0. There are two ways in which you can obtain I/O status information.
int rdstate ( );
rdstate function returns the current status of the error flags encoded into an integer. It
returns zero, when no error has occurred. Otherwise, an error bit is turned on.
Method Description
int bad ( ) Returns true if badbit is set.
int fail ( ) Returns true if failbit is set.
int eof ( ) Returns true if there are no errors.
int good ( ) Otherwise they return false.
Once an error has occurred, it may need to be cleared before your program continues. to
do this, use the clear ( ) method.
- 15 -
(which writes a single character) is called, the character is not written directly to the
physical file with which the stream is associated. Instead of that, the character is inserted
in that stream's intermediate buffer.
When the buffer is flushed, all the data contained in it is written to the physical medium (if
it is an output stream) or simply freed (if it is an input stream). This process is called
synchronization and takes place under any of the following circumstances:
When the file is closed: before closing a file all buffers that have not yet been
flushed are synchronized and all pending data is written or read to the physical
medium.
When the buffer is full: Buffers have a certain size. When the buffer is full it
is automatically synchronized.
- 16 -
Exercise
1. Write a program that accept N student record from the keyboard & store the list on a
file “D:\\ Test.txt” in a text file format
2. Write a program that reads students record from the text file “D:|\ Test.txt” and display
on the screen.
3. Do Q1 in binary format.
4. Do Q2 in binary format.
Note Student record consists of first name, last name, gender, age and Id.
Solution
Consider the file Header .h which contains the basic preprocessing include files,
structure definition and functions other than the main function.
Header. h // this file is saved at D: and it has the following structure.
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
struct studList
{
char firstName[12];
char lastName[12];
int age;
char gender;
char Id[12];
};
studList getStudent()
{
studList std;
cout<<"Enter student first name ===>";cin>>std.firstName;
cout<<"Enter student last name ===>";cin>>std.lastName;
cout<<"Enter student age ===>";cin>>std.age;
cout<<"Enter student gender ===>";cin>>std.gender;
cout<<"Enter student Id ===>";cin>>std.Id;
return std;
}
void DisplayStudent(studList std)
{
cout<<"Student first name:\t"<<std.firstName<<endl;
cout<<"Student last name :\t"<<std.lastName<<endl;
cout<<"Student age:\t\t"<<std.age<<endl;
cout<<"Student gender:\t\t"<<std.gender<<endl;
cout<<"Student Id:\t\t"<<std.Id<<endl;
}
Solution 1
#include "D:\ header.h"
int main() {
studList std;
- 17 -
fstream outf;
outf.open("d:\\test.txt",ios::app);
if(outf.fail()) {
cout<<"unable to open the file d:\test.txt\n";
return 1;
}
clrscr();
int N;
cout<<"Enter the number of students ===> ";cin>>N;
for(int i = 0; i < N; i++) {
std = getStudent();
clrscr();
outf<<std.firstName<<" "<<std.lastName<<" "<<std.Id<<" "
<<std.gender<<" "<<std.age<<endl;
}
getch();
return 0;
}
Solution 2
#include "D:\ header.h"
int main(){
studList std;
fstream inpf;
inpf.open("d:\\test.txt",ios::in);
if(inpf.fail())
{
cout<<"unable to open the file d:\test.txt\n";
return 1;
}
clrscr();
while (!inpf.eof())
{
inpf>>std.firstName>>std.lastName>>std.Id>>std.gender>>std.age;
if(inpf.eof()) break;
DisplayStudent(std);
cout<<"=====================================\n";
getch();
}
inpf.close();
return 0;
}
Solution 3
#include "D:\ header.h"
int main()
{
studList std;
fstream outf;
outf.open("d:\\test2.txt",ios::app|ios::binary);
if(outf.fail())
{
cout<<"unable to open the file d:\test.txt\n";
return 1;
}
clrscr();
- 18 -
int N;
cout<<"Enter the number of students ===> ";cin>>N;
for(int i = 0; i < N; i++)
{
std = getStudent();
clrscr();
outf.write((char *) &std, sizeof(std));
}
getch();
return 0;
}
Solution 4
#include "D:\ header.h"
int main() {
studList std;
fstream inpf;
inpf.open("d:\\test2.txt",ios::in|ios::binary);
if(inpf.fail())
{
cout<<"unable to open the file d:\test.txt\n";
return 1;
}
clrscr();
while (!inpf.eof()){
{
inpf.read((char*)&std, sizeof(std));
//inpf>>std.firstName>>std.lastName>>std.Id>>std.gender>>std.age;
if(inpf.eof()) break;
DisplayStudent(std);
cout<<"======================================\n";
getch();
}
inpf.close();
return 0;
}
- 19 -
// Function to write student data to a file
void writeStudentToFile(const Student& student, const string& filename) {
ofstream outfile(filename, ios::app); // Open file in append mode
- 20 -
// Close the file
infile.close();
}
int main() {
// Create a few student objects
Student student1 = {"Alice", 20, "Computer Science"};
Student student2 = {"Bob", 21, "Mathematics"};
return 0;
}
Explanation:
We define a struct called Student to hold information about a student, including their
name, age, and major. We define two functions: writeStudentToFile to write student data
to a file, and readStudentsFromFile to read student data from a file and display it. In
writeStudentToFile, we open the file in append mode and write the student's name, age,
and major to the file separated by commas. In readStudentsFromFile, we read each line
from the file, parse it to extract student attributes, and then print the information to the
console. In main, we create two Student objects, write their data to a file using
writeStudentToFile, and then read and display the student data using
readStudentsFromFile.
- 21 -
Annex
- 22 -
int good ( );
void clear ( int flags = 0 ) ;
- 23 -