File Handling
File Handling
Pooja Jain
Stream
ofstream outfile(“sample.txt”);
ifstream infile(“sample.txt”);
Single file stream working on one file
Disk
Output stream
file
outfile sample.txt
Program
Input stream
infile
Program: Writing and reading data into file, using constructors
# include <fstream.h> Opening the file sample.txt in the
void main() output mode (to write the data into it)
{
ofstream outfile(“sample.txt”); // create file for output
char ch = ‘a’;
int i = 12;
float f = 4356.15;
char arr[ ] = “hello”;
outfile << ch << endl <<i << endl << f << endl << arr; //send the data to file
outfile.close(); Closing the file, using close() method
infile >> ch >> i >> f >> arr; // read data from file
ifstream outfile;
outfile.open("cpp-home.txt");
Parameter Meaning
ios::app Append to end of the file
ios::ate Go to end of the file on opening
ios::in Open file for reading only
ios::nocreate Open fails if the file does not exist
ios::noreplace Open fails if the file already
exists
ios::out Open file for writing only
ios::trunc Delete contents of the file if it
Cont.
Opening a file in ios::out mode also opens it in the ios::trunc mode by
default.
Both ios::app and ios::ate take us to the end of the file when it is
opened. But the difference between the two parameters is that the
ios::app allows us to add data to the end of the file only, while ios:: ate
mode permits us to add data or to modify the existing data anywhere in
the file.
Creating a stream using ifstream implies input and creating a stream
using ofstream implies output. So in these cases it is not necessary to
provide the mode parameters.
The fstream class does not provide a mode by default and thus, we
must provide the mode explicitly when using an object of fstream class.
The mode can combine two or more parameters using the bitwise OR
operator
fstream fout ;
fout.open(“data”, ios::app | ios::nocreate);
This opens the file in the append mode but fails to open the file if it
does not exist.
Closing the file:
We have specifically closed the file once writing
is over by calling the function close(). This is
necessary since we wanted to open the same
file for reading.
#include<fstream.h>
void main()
{
const int MAX=80;
char arr[MAX];
ifstream infile(“sample1.txt”); // Open the file in read (input) mode
while(infile) // until end of file
{
infile.getline(arr, MAX); // Reads first line from the file and stores
// it into the array arr
cout << arr; // Displaying the first line onto the console
}
}
Character i/o
void main()
{
char str[]=“C++ is superset of C. It is an object-oriented /
programming language.”;
#include<fstream.h>
void main()
{
char ch;
ifstream infile(“sample2.txt”); // Open the same file in input mode
while(infile!= EOF)
{
inflie.get(ch); // Read the data from the file character by character
the // and store the character into the variable ch
cout << ch; // Display the character read, onto the screen
}
}
EOF
while(!infile.eof()) or while(infile!= EOF)– The
function eof() returns a nonzero value if the end of
the file has been reached. So, we make a while
loop, that will loop until we reach the end of the file.
So, we will get through the whole file, so that we can
read it!
infile is the object from class ifstream.This class
declares a function called get(). So, we can use this
function, as long as we have an object. The get()
function extracts a single character from the stream
and returns it.
August 21, 2018 Pooja Jain 23
Writing and reading Objects of a class
So far we have done I/O of basic data types. Since the class objects are
the central elements of C++ programming, it is quite natural that the
language supports features for writing and reading from the disk files
objects directly.
The binary input and output functions read() and write() are designed to
do exactly this job.
The write() function is used to write the object of a class into the
specified file and read() function is used to read the object of the class
from the file.
Both these functions take two arguments:
1. address of object to be written.
2. size of the object.
The address of the object must be cast to the type pointer to char.
One important point to remember is that only data members are written
to the disk file and the member functions are not.
Example: Writing an object into the file
class Person
{
private:
char name[40];
int age;
public:
void getData()
{
cout << “\n Enter name:”; cin >> name;
cout << “\n Enter age:”; cin >> age;
}
} ; // End of the class definition
Cont.
void main()
{
Person per ; // Define an object of Person class
}
} // End of the main function
File pointers and their manipulations:
Each file has two associated pointers known as the file
pointers.
infile.seekg(10);
Moves the file pointer to the byte number 10.
ofstream fileout;
fileout.open(“sample.txt”, ios::app);
int p = fileout.tellp();
class person
{
private:
char name[40];
int age;
public:
void showData()
{
cout << “\n Name = “ << name;
cout << “\n Age = “ << age;
}
};
Cont.
void main()
{
person pers; // create person object
ifstream infile; // create input file
infile.open(“Person.txt”); // open the file
infile.seekg(0, ios::end); // go to end
int endposition = infile.tellg(); // find where we are
int n = endposition/sizeof(person); // number of persons
cout << “\n There are “ << n << “ persons in file: “;
cout << “\n Enter person number: “;
cin >> n;
int position = (n-1) * sizeof(person); // number times size
infile.seekg(position);
infile.read( (char*)&pers, sizeof(pers) );
pers.showData(); // display the person
}