0% found this document useful (0 votes)
55 views10 pages

Basic Operation On Text File in C

The document discusses various operations that can be performed on text files in C++, including writing to a text file, reading from a text file, counting the number of characters, words, and lines in a file, and copying the contents of one file to another. It also covers basic operations on binary files in C++ such as writing to a binary file, displaying records from a binary file, searching for and displaying a record, deleting a record, and modifying a record. The code examples provided demonstrate how to implement these various file operations using C++.

Uploaded by

Danial Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views10 pages

Basic Operation On Text File in C

The document discusses various operations that can be performed on text files in C++, including writing to a text file, reading from a text file, counting the number of characters, words, and lines in a file, and copying the contents of one file to another. It also covers basic operations on binary files in C++ such as writing to a binary file, displaying records from a binary file, searching for and displaying a record, deleting a record, and modifying a record. The code examples provided demonstrate how to implement these various file operations using C++.

Uploaded by

Danial Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Basic Operation On Text File In C++

Program to write in a text file

#include<fstream>

int main()

{
ofstream fout;\
fout.open("out.txt");
char str[300]="Time is a great teacher
but unfortunately it kills all its pupils.
Berlioz";
fout<<str;
fout.close();
return 0;
}

Program to read from text file and display it


#i
nclude<fstream.h>
#include<conio.h>
int main()
{
ifstream fin;
fin.open("out.txt");
char ch;
while(!fin.eof())
{
fin.get(ch);
cout<<ch;
}
fin.close();
getch();
return 0;
}
Program to count number of characters.
#include<fstream.h>
#include<conio.h>
int main()
{
ifstream fin;
fin.open("out.txt");
clrscr();
char ch; int count=0;
while(!fin.eof())
{
fin.get(ch);
count++;
}
cout<<"Number of characters in file is "<<count;
fin.close();
getch();
return 0;
}
Program to count number of words
#include<fstream.h>
#include<conio.h>
int main()
{
ifstream fin;
fin.open("out.txt");
char word[30]; int count=0;
while(!fin.eof())
{
fin>>word;
count++;
}
cout<<"Number of words in file is "<<count;
fin.close();
getch();
return 0;
}
Program to count number of lines
#include<fstream.h>
#include<conio.h>
int main()
{
ifstream fin;
fin.open("out.txt");
char str[80]; int count=0;
while(!fin.eof())
{
fin.getline(str,80);
count++;
}
cout<<"Number of lines in file is "<<count;
fin.close();
getch();
return 0;
}
Program to copy contents of file to another file.
#include<fstream.h>
int main()
{
ifstream fin;
fin.open("out.txt");
ofstream fout;
fout.open("sample.txt");
char ch;
while(!fin.eof())
{
fin.get(ch);
fout<<ch;
}
fin.close();
return 0;
}

Basic Operation On Binary File In C++


class student
{
int admno;
char na
me[20];
public:
void getdata()
{
cout<<"\nEnter The admission no. ";
cin>>admno;
cout<<"\n\nEnter The Name of The Student
";
gets(name);
}
void showdata()
{
cout<<"\nAdmission no. : "<<admno;
cout<<"\nStudent Name : ";
puts(name);
}
int retadmno()
{
return admno;
}
};

function to write in a binary file


void write_data()
{
student obj;
ofstream fp2;
fp2.open("student.dat",ios::binary|ios::app);
obj.getdata();
fp2.write((char*)&obj,sizeof(obj));
fp2.close();
}
function to display records of file
void display()
{
student obj;
ifstream fp1;
fp1.open("student.dat",ios::binary);
while(fp1.read((char*)&obj,sizeof(obj)))
{
obj.showdata();
}
}
fp.close();
}
Function to search and display from binary file
void search (int n)
{
student obj;
ifstream fp1;
fp1.open("student.dat",ios::binary);
while(fp1.read((char*)&obj,sizeof(obj)))
{
if(obj.retadmno()==n)
obj.showdata();
}
fp1.close();
}
Function to delete a record
void deleterecord(int n)
{
student obj;
ifstream fp1;
fp1.open("student.dat",ios::binary);
ofstream fp2;
fp2.open("Temp.dat",ios::out|ios::binary);
while(fp1.read((char*)&obj,sizeof(obj)))
{
if(obj.retadmno()!=n)
fp2.write((char*)&obj,sizeof(obj));
}
fp1.close();
fp2.close();
remove("student.dat");
rename("Temp.dat","student.dat");
}
Function to modify a record
void modifyrecord(int n)
{
fstream fp;
student obj;
int found=0;
fp.open("student.dat",ios::in|ios::out);
while(fp.read((char*)&obj,sizeof(obj)) &&
found==0)
{
if(obj.retadmno()==n)
{
obj.showdata();
cout<<"\nEnter The New Details of
student";
obj.getdata();
int pos=-1*sizeof(obj);
fp.seekp(pos,ios::cur);
fp.write((char*)&obj,sizeof(obj));
found=1;
}
}
fp.close();
}

You might also like