Beruflich Dokumente
Kultur Dokumente
#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();
}
#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");
}
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:
Leave a comment
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();
10
here are different modes of opening a file in C++ which are:
#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