Chapter - 6 - Programming II
Chapter - 6 - Programming II
6.1 Introduction
The data created by the user and assigned to variables with an
assignment
statement is sufficient for some applications. With large volume of data
most real-world applications use a better way of storing that data. For
this,
disk files offer the solution.
When working with disk files, C++ does not have to access much RAM
because C++ reads data from your disk drive and processes the data
only
parts at a time.
6.2 Stream
Stream is a general name given to flow of data. In C++, there are
different
types of streams. Each stream is associated with a particular class,
which
contains member function and definition for dealing with file. Lets have
a
look at the figure:
According to the above hierarchy, the class iostream is derived from
the two classes’ istream and ostream and both istream and ostream
are derived from ios. Similarly the class fstream is derived from
iostream.
Generally two main header files are used iostream.h and fstream.h.
The
classes used for input and output to the video display and key board
are
declared in the header file iostream.h and the classes used for disk file
input output are declared in fstream.h.
Note that when we include the header file fstream.h in our
program then
there is no need to include iostream.h header file. Because all
the classes
which are in fstream.h they are derived from classes which are in
iostream.h therefore, we can use all the functions of iostream class.
First we will see how files are opened and closed. A file can be defined
by
following class ifstream, ofstream, fstream, all these are defined in
fstream.h header file.
if a file object is declared by ifstream class, then that object can be
used for reading from a file.
if a file object is declared by ofstream class, then that object can
be used for writing onto a file.
If a file object is declared by fstream class then, that object can be
used for both reading from and writing to a file.
If you open a file for writing (out access mode), C++ creates the file. If
a
file by that name already exists, C++ overwrite the old file with no
warning. You must be careful when opening files not to overwrite
existing
data that you want.
If an error occurs during opening of a file, C++ does not create a valid
file
pointer (file object). Instead, C++ creates a file pointer (object) equal
to
zero. For example if you open a file for output, but use an invalid disk
name, C++ can’t open the file and therefore makes the file object
equal to
zero.
You can also determine the file access mode when creating a file in C+
+. If
you want to use the open function to open a file then the syntax is:
fileobject.open(filename,accessmode);
o File name is a string containing a valid file name for
your computer.
o Accessmode is the sought operation to be taken on
the file and must be one of the values in the
following table.
o outdata.open(“c:\\names.txt”,ios::app);
o outdata.open(“c:\\names.txt”,ios::in);
o outdata.open(“c:\\names.txt”,ios::out);
You should always check for the successful opening of a file before
starting file manipulation on it. You use the fail() function to do the
task:
Lets have an example here:
ifstream indata;
indata.open(“c:\\myfile.txt”,ios::in);
if(indata.fail())
{
//error description here
}
In this case, the open operation will fail (i.e the fail function will return
true), if there is no file named myfile.txt in the directory C:\
After you are done with your file manipulations, you should use the
close
function to release any resources that were consumed by the file
operation.
Here is an example
indata.close();
The above close() statement will terminate the relationship between
the
ifstream object indata and the file name “c:\myfile.txt”, hence
releasing
any resource needed by the system.
#include<fstream>
#include<conio>
#include<stdlib>// for exit() function
void main()
{
char c;
ofstream outfile;
outfile.open("D:\\test.txt",ios::out);
if(outfile.fail())
{
cout<<"\nError opening test.txt";
getch();
exit(1);
}
for(int i=1;i<=15;i++)
{
cout<<"\nEnter a character : ";
cin>>c;
outfile.put(c);// used to write a line or string to the output
}
outfile.close();
}//end main
#include<fstream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
char c,filename[15];
ifstream indata;
cout<<"\nEnter the file name : ";
cin.getline(filename,15);
indata.open(filename,ios::in);
if(indata.fail())// check id open succeeded
{
cerr<<"\nError opening file : "<<filename;
getch();
exit(1);
}
while(!indata.eof())// check for eof
{
indata.get(c);
cout<<c;
}
indata.close();
getch();
}
1. seekg( )
2. seekp( )
3. tellg( )
4. tellp( )
1. seekg ( ): this moves get pointer i.e input pointer to a specified
location.
For eg. infile.seekg(5); move the file pointer to the byte number
5
from starting point.
2. seekp( ): this move put pointer (output pointer) to a specified
location for
example: outfile.seekp(5);
3. tellg( ): this gives the current position of get pointer (input
pointer)
4. tellp( ): this gives the current position of put pointer (output
pointer)
eg. ofstream fileout;
fileout.open(“c:\\test.txt”,ios::app);
int length = fileout.tellp();
To point to the end of a data file, you can use the seekg()
function to
position the file pointer at the last byte. This statement positions
the file
pointer to the last byte in the file. Fileobj.seekg(0L,ios::end);
This seekg() function literally reads “move the file pointer 0
bytes from the
end of the file.” The file pointer now points to the end-of-file
marker, but
you can seekg() backwards to find other data in the file.
The following program is supposed to read “c:\alph.txt” file
backwards,
printing each character as it skips back in the file.
Be sure that the seekg() in the program seeks two bytes
backwards from
the current position, not from the beginning or the end as the
previous
programs. The for loop towards the end of the program needs to
perform
a “skip-two-bytes-back”, read-one-byte-forward” method to skip
through
the file backwards.
#include<fstream.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
clrscr();
ifstream indata;
int ctr=0;
char inchar;
indata.open("c:\\alph.txt",ios::in);
if(indata.fail())
{
cerr<<"\nError opening alph.txt";
getch();
exit(1);
}
indata.seekg(-1L,ios::end);//points to the last byte in the file
for(ctr=0;ctr<26;ctr++)
{
indata>>inchar;
indata.seekg(-2L,ios::cur);
cout<<inchar;
}
indata.close();
getch();
}
A binary file is one that does not contain text. It is used to store data in
the form of bytes, which are typically interpreted as something other
than textual characters.
The default access mode for file access is text mode. A text file is an
ASCII
file, compatible with most other programming languages and
applications. Programs that read ASCII files can read data you create
as C++ text files.
If you specify binary access, C++ creates or reads the file in binary
format.
Binary data files are “squeezed”- that is, they take less space than text
files.
The disadvantage of using binary files is that other programs can’t
always
read the data files. Only C++ programs written to access binary files
can
read and write them. The advantage of binary files is that you save
disk
space because your data files are more compact.
The binary format is a system-specific file format. In other words, not
all
computers can read a binary file created on another computer.
The above code uses the combination of ostream function seekp and
write to store data at exact locations in the file.
Function seekp sets the put file-position pointer to a specific position in
the file, then the write outputs the data.
1 is subtracted from the student id when calculating the byte location
of the record. Thus, for record 1, the file position pointer is set to the
byte 0 of the file.
The istream function read inputs a specified number of bytes from the
current position in the specified stream into an object.
The syntax of read : read((char*)&name of object, sizeof(name of
object));
Function read requires a first argument of type char *. The second
argument of write is an integer of type size specifying the number of
bytes to be read.
Here is a code that shows how to read a random access record from a
file.
#include<fstream.h>
#include<conio.h>
#include<stdlib.h>
struct stud_info{
int studid;
char name[20];
char fname[20];
float CGPA;
};
void main()
{
clrscr();
ifstream indata;
char filename[15];
cout<<"\nEnter the file name : ";
cin>>filename;
indata.open(filename,ios::in);
if(indata.fail())
{
cerr<<"\nError opening "<<filename;
getch();
exit(1);
}
stud_info student;
cout<<"\nEnter the id no of the student : ";
int sid;
cin>>sid;
indata.seekg((sid-1) * sizeof(student));
indata.read((char*) &student, sizeof(student));
cout<<"\nhere is the information";
cout<<"\nstudent id : "<<student.studid;
cout<<"\nstudent name : "<<student.name;
cout<<"\nstudent fname : "<<student.fname;
cout<<"\nstudent CGPA : "<<student.CGPA;
indata.close();
getch();
}
{
ofstream outdata;
ifstream indata;
outdata.open(argv[2],ios::out);
if(outdata.fail())
{
cerr<<"\nlow disk space to create file : "<<argv[2];
getch();
exit(1);
}
indata.open(argv[1],ios::in);
if(indata.fail())
{
cerr<<"\nfile : "<<argv[1]<<" does not exist";
getch();
exit(1);
}
//now start copying the file
while(!indata.eof())
{
indata.get(ch);
outdata.put(ch);
}
indata.close();
outdata.close();
cout<<"\nfinished copying";
}
}