FILE HANDLING v2
FILE HANDLING v2
FILE:
1. A file is a collection of related data stored in a particular area on the disk.
2. A program involves either or both of the following kinds of data communication-
� Data transfer between console unit and the program.
� Data transfer between the program and a disk file.
3. The Input Stream extracts or reads data from the file and the Output Stream
inserts or writes data to the file.
STEPS INVOLVED:
Create a file stream object to manage the stream using the appropriate class. i.e.
Ofstream is used to create the output stream and the class Ifstream is used to create
the input stream.
E.g.–1) <classname><objectname>;
student obj1;
ofstream outfile(“results”);
Here ofstream is the file stream class and outfile is the object of the class ofstream.
The above statement will open the file named “results” for the output.
Ifstream infile(“data”);
The above statement will open the file named “data” as an input file.
void main()
{
ofstream outf("student"); //creating the file named "student" using ofstream class
int roll_no;
char name[30];
ifstream inf("student"); //opening the "student" file in the input mode using ifstream
class
getch();
outf.open(“student”);
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
ofstream fout; //creating an object fout for the output stream ofstream class
char buffer[100]; //declaring a buffer of size 100 to store data read from the file
getch();
}
1. Detection of the end of the file is necessary for preventing any further
attempt to read data from the file.
Example:-
while( fstream object)
while(fin)
Here, fin is the ifstream object. It returns 0 if any error occurs in the file
operation including end of file condition. Hence, the while loop will terminate
when fin will return a 0 on reaching the end of file condition.
2. There is another way to detect the end of file condition. It can be done by the
following statement:-
if (fin.eof( ) != 0)
{
exit(1);
}
Here, eof( ) is a member function of the class ios. It will return a non-zero
value if the end-of-file (EOF) condition is encountered and a zero otherwise.
Hence, the above statement will terminate the program on reaching the end
of the file.
FILE MODES
1. The function open( ) can also take 2 arguments, the first argument is the
“filename” and the second argument is used for specifying the file mode.
Here, the 2nd argument specifies the purpose for which the file is opened.
2. If the 2nd argument is not specified then how the file is opened. In this
case, it will take default values:
ios::in for ifstream functions i.e. open for reading only
ios::out for ofstream functions i.e. for writing only
Parameter Meaning
FILE POINTERS
Example:-
ofstream outf;
outf.open(“abc”, ios::app);
int p = outf.tellp( );
Here, the above statements will move the output pointer put to the end of the file
named “abc” and the value of p will represent the total number of bytes in the file
as tellp( ) will return the current position of the pointer and currently the pointer
is at the end of the file.
⮚ “Seek” functions like seekg( ) and seekp( ) can also be used with two
arguments as follows:-
seekg(offset, refposition);
seekp(offset, refposition);
The parameter “offset” represents the number of bytes the file pointer is to be
moved from the location specified by the parameter refposition.
The refposition can take one of the following constants defined in the ios class:-
1. ios::beg - start of the file
2. ios::cur - current position of the pointer
3. ios::end - end of the file
void main()
{
char string[100]; // creating a character array of size 100
int len=strlen(string); // calculating the length of the entered string and storing it in variable len
char ch;
getch();
}
void main()
{
ofstream fout; //creating an output sream class object fout
int count=0; // initializing count variable to 0
while(fin.eof()==0)
{
char buffer[100];
fin.getline(buffer,100); //reading from the file line by line
cout<<buffer<<”\n”; //displaying to the screen
count++; //incrementing the value for count
}
fin.close(); //closing the file in the read mode
cout<<count;
getch();
}
Write a program to count the number of words in a file and display on
the screen.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
clrscr();
int count=0;
ofstream outf;
outf.open("ABC");
outf<<"My name is divya";
outf.close();
ifstream inf;
inf.open("ABC");
char buffer[100];
while(inf.eof()==0)
{
inf.getline( buffer, 100, ' ' ); //using getline delimiter (It will fetch character until ‘ ‘
is encountered or 100 characters are encountered)
cout<<buffer;
count++;
}