Sie sind auf Seite 1von 12

File Handlin in C++

When ever we store a value in a variable, that value is stored


in the RAM and is lost when we turn off the power to the
computer. But we may have some work for that data later.
For this we use File Handling, in which we store the value in a
file on the hard disk so, that it can be used for future
reference.
For this we generally use text ( .txt ) and binary ( .dat )
files and we need to include another header file fstream.h. It
is inherited from iostream.h so in a program only fstream.h is
included.
For example:
//To open a text file in write mode:

#include<fstream.h>
#include<conio.h>
void main()
{
char ch;
fstream fp;
fp.open( “abc.txt”,ios::out); //This line opens the text
file in write mode
clrscr();
cout<<“Enter a character”;
cin>>ch;
fp.put(ch); // This line writes whatever the value in the
variable ch into the file abc.txt
fp.close();
}

1
In C++ we have a get pointer and a put pointer for getting (i.e. reading)
data from a file and putting(i.e. writing) data on the file respectively.
seekg() is used to move the get pointer to a desired location with respect to
a reference point.
Syntax: file_pointer.seekg (number of bytes ,Reference point);
Example: fin.seekg(10,ios::beg);
tellg() is used to know where the get pointer is in a file.
Syntax: file_pointer.tellg();
Example: int posn = fin.tellg();
seekp() is used to move the put pointer to a desired location with respect to
a reference point.
Syntax: file_pointer.seekp(number of bytes ,Reference point);
Example: fout.seekp(10,ios::beg);
tellp() is used to know where the put pointer is in a file.
Syntax: file_pointer.tellp();
Example: int posn=fout.tellp();
The reference points are:
ios::beg – from beginning of file
ios::end – from end of file
ios::cur – from current position in the file.
In seekg() and seekp() if we put – sign in front of number of bytes then we can move
backwards.

2
//To display nth Record from a Binary file.

#include<fstream.h>
#include<conio.h>
class student{
int rollno;
char name[20];
float percent;
public:
void getdata(){
cout<<"Enter Roll number";
cin>>rollno;
cout<<"Enter Name";
gets(name);
cout<<"Enter Percentage";
cin>>percent;
}
void showdata(){
cout<<"Roll number: "<<rollno<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Percent: "<<percent<<endl;
}
int rrollno(){
return(rollno);
}
};
void main(){
fstream fin;
student s;
int size,r,n;
fin.open("Student.dat",ios::in|ios::binary);
cout<<"Enter the number of record to be Displayed";
cin>>n;
fin.seekg(0,ios::end);
size=fin.tellg();
r=size/sizeof(s);
if (n<=r){
fin.seekg((n-1)*sizeof(s),ios::beg);
3
fin.read((char *)&s,sizeof(s));
s.showdata();
}
else
cout<<"Record does not exist";
fin.close();
}

//To delete nth record from a binary file

#include<fstream.h>
#include<conio.h>
#include<dos.h>
class student{
int rollno;
char name[20];
float percent;
public:
void getdata(){
cout<<"Enter Roll number";
cin>>rollno;
cout<<"Enter Name";
gets(name);
cout<<"Enter Percentage";
cin>>percent;
}
void showdata(){
cout<<"Roll number: "<<rollno<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Percent: "<<percent<<endl;
}
int rrollno(){
return(rollno);
}
};
void main()
{
fstream fin,fout;
4
fin.open("Student.dat",ios::in|ios::binary);
fout.open("temp.dat",ios::out|ios::binary);
student s;
int r,n,c=1;
cout<<"Enter record number to be deleted";
cin>>n;
fin.seekg(0,ios::end);
r=(fin.tellg()/sizeof(s));
if (n<=r){
while (fin){
fin.read((char *)&s,sizeof(s));
if (c!=n){
fout.write((char *)&s,sizeof(s));
}
c++;
}
}
else
cout<<"Record does not exist";
fin.close();
fout.close();
unlink("Student.dat");
rename("temp.dat","Student.dat");
}

//To Modify nth record


#include<fstream.h>
#include<conio.h>
class student{
int rollno;
char name[20];
float percent;
public:
void getdata(){
cout<<"Enter Roll number";
cin>>rollno;
cout<<"Enter Name";
gets(name);
5
cout<<"Enter Percentage";
cin>>percent;
}
void showdata(){
cout<<"Roll number: "<<rollno<<endl;
cout<<"Name: "<<name<<endl;
cout<<"Percent: "<<percent<<endl;
}
int rrollno(){
return(rollno);
}
};
void main(){
fstream file;
file.open("Student.dat",ios::in|ios::out|ios::binary);
student s;
int n,r;
char ch;
cout<<"Record to be Modified";
cin>>n;
file.seekg(0,ios::end);
r=(file.tellg()/sizeof(s));
if( n<=r){
file.seekg((n-1)*sizeof(s),ios::beg);
file.read((char *)&s,sizeof(s));
s.showdata();
cout<<"Modify?";
cin>>ch;
if(ch=='y'||ch=='Y'){
file.seekp((n-1)*sizeof(s),ios::beg);
s.getdata();
file.write((char *)&s,sizeof(s));
}
}
else
cout<<"Record does not exist";
file.close();
}
6
//Program to Add n number of records into a binary file.
#include<fstream.h>
#include<conio.h>
class student{
int rollno;
char name[20];
float percent;
public:
void getdata(){
cout<>rollno;
cout<<"Enter name";
gets(name);
cout<>percent;
}
void showdata(){
cout<<"Roll number"<<rollno<<endl;
cout<<"Name:"<<name<<endl;
cout<<"Percent:"<<percent<<endl;
}
int rrollno(){
return(rollno);
}
};
void main()
{
student s;
fstream fout;
fout.open("Student.dat",ios::out|ios::binary);
char ch;
do {
cout<<"Enter details of student";
s.getdata();
cout<<"Do you want to Add more Records?(y/n)"
cin>>ch;
} while (ch=='y'||ch=='Y');
fout.close();
}

7
/*Program to Enter roll number and search for it in the file and
if found display the details of the student.
The Class is same as before*/

#include<fstream.h>
#include<conio.h>
class student{
int rollno;
char name[20];
float percent;
public:
void getdata(){
cout<>rollno;
cout<<"Enter name";
gets(name);
cout<>percent;
}
void showdata(){
cout<<"Roll number"<<rollno<<endl;
cout<<"Name:"<<name<<endl;
cout<<"Percent:"<<percent<<endl;
}
int rrollno(){
return(rollno);
}
};
void main()
{
fstream fin;
student s;
fin.open("Student.dat",ios::in|ios::binary);
int rn, flag=0;
cout<<"Enter roll number to be searched";
cin>>rn;
while (fin)
{
fin.read((char *)&s,sizeof(s));
if(rn==s.rrollno)
{
s.showdata();

8
flag=1;
}
if( flag==0)
cout<<"Record not found";
fin.close();
}
Share this:

 Click to share on Twitter (Opens in new window)


 Share on Facebook (Opens in new window)
 Click to share on Google+ (Opens in new window)

Leave a comment

seekg(), tellg(), seekp(), tellp()


April 30, 2015 File Handling in C++ C++, File, Handling

In C++ we have a get pointer and a put pointer for getting (i.e.
reading) data from a file and putting(i.e. writing) data on the
file respectively.
seekg() is used to move the get pointer to a desired location
with respect to a reference point.
Syntax: file_pointer.seekg (number of bytes ,Reference
point);
Example: fin.seekg(10,ios::beg);
tellg() is used to know where the get pointer is in a file.
Syntax: file_pointer.tellg();
Example: int posn = fin.tellg();

seekp() is used to move the put pointer to a desired location


with respect to a reference point.
Syntax: file_pointer.seekp(number of bytes ,Reference
point);
Example: fout.seekp(10,ios::beg);
tellp() is used to know where the put pointer is in a file.
9
Syntax: file_pointer.tellp();
Example: int posn=fout.tellp();
The reference points are:
ios::beg – from beginning of file
ios::end – from end of file
ios::cur – from current position in the file.
In seekg() and seekp() if we put – sign in front of number of
bytes then we can move backwards.
From the examples it is clear that these functions are very much
alike.

10
here are different modes of opening a file in C++ which are:

1. Write mode- As the name suggests this mode is used to write in a


file. It will create the file if it does not exist and deletes
all the contents of the file and starts writing from the
beginning if the file exists.

#include <fstream.h>
#include <conio.h>
void main()
{
char ch;
fstream fout;
clrscr();
fout.open("abc.txt",ios::out);
ch=getch();
fout.put(ch);
getch();
fout.close();
}
2. Read mode- As the name suggests this mode is used to read from a
file. It will not create the file if it does not exist.
#include <fstream.h>
#include <conio.h>
void main()
{
fstream fin;
char ch;
fin.open("abc.txt",ios::in);
clrscr();
while (!fin.eof())
{
fin.get(ch);
cout<<ch;
}
fin.close();
}

This program reads a file till its end. eof= end of file.
3. Append mode- This mode is used to add to the content of the file
and not delete its previous content as in write mode. It writes
from the end of file.
#include <fstream.h>
11
#include <conio.h>
void main()
{
fstream fapp;
char ch;
fapp.open("abc.txt",ios::app);
clrscr();
ch=getch();
fapp.put(ch);
getch();
fapp.fclose();
}

12

Das könnte Ihnen auch gefallen