In C++, sequential read and write operations are commonly performed on files using the fstream library.
Sequential
Read / Write Operations
•
In C++, sequential read and write
operations are commonly performed on files using the fstream library. These operations mean data is accessed in the same
order it is stored.
•
For writing to the file, we use the insertion operator << with an ofstream object.
•
That means << is used to send data
into the file stream.
• The
ofstream is the output file stream class and hence we have to write a line #include<fstream> at the
beginning of our C++ program.
C++
Program
#include<iostream>
#include<fstream>
using namespace std;
int main() {
ofstream
outFile("myfile.txt");
if(!outFile) {
cout<<"Error
opening File!!!"<<endl;
return 1;
}
outFile<<"This
is first line"<<endl;
outFile<<"This
is second line"<<endl;
outFile
<<"This is the last line"<<endl;
outFile.close();
cout<<"Data written
successfully!!!"<<endl;
return 0;
}
Data written
successfully!!!
•
For reading a file in C++, we use an object of the ifstream class.
•
The ifstream class is defined in the <fstream> header file, so we must
include it at the beginning of the program.
• We need to open the file by passing the file
name in the constructor
•
Then, we can read from the file using getline(). This function is useful to
read the file line by line.
•
Finally, we should close the file with
.close() after reading.
Demo example for
sequential file handling pago
For
updating the contents of the file, we must position the file pointer at the
desired record's position. For that purpose we use seekg command is used. Similarly, we can update a file using
delete and append operations. The delete operation deletes the existing record.
The append operation inserts the record at the end of the file.
Following
program shows various operations such as update, delete, append
/*****************************************************************
Program for performing various operations on Sequential File
organization.
******************************************************************/
#include<iostream>
#include<iomanip>
#include<fstream>
#include<cstring>
#include<cstdlib>
using namespace std;
class EMP_CLASS
{
typedef struct EMPLOYEE 
{
char name[10];
int emp_id;
int salary;
}Rec;
Rec Records;
public:
void Create();
void Display();
void Update();
void Delete();
void Append();
int Search();
};
void EMP_CLASS::Create()
{
char ch='y';
fstream seqfile;
seqfile.open("EMP.DAT",ios::in
| ios::out | ios::binary);
do
{

//User must enter the desired data in the members of te structure
Records
cout<<"\n Enter Name:
cin>>Records.name;
cout<<"\n Enter Emp_ID:
";
cin>>Records.emp_id;
cout<<"\n Enter Salary:
";
cin>>Records.salary;
//then write the record containing this data in the file
seqfile.write((char*)&Records,sizeof(Records));
cout<<"\nDo
you want to add more records?";
cin>>ch;
} while(ch=='y');
seqfile.close();
}
void EMP_CLASS::Display()
{
fstream seqfile;
int n,m,i;
seqfile.open("EMP.DAT",ios::in | ios::out |
ios::binary);
//positioning
the pointer in the file at the beginning
seqfile.seekg(0,ios::beg);
cout<<"\n The Contents of file are...
..."<<endl;
//read
the records sequentially
while(seqfile.read((char *)&Records,sizeof(Records)))
{
if(Records.emp_id!= ‒1) edo biose
{
cout<<"\nName:
"<<Records.name;
cout<<"\nEmp_ID:
"<<Records.emp_id;
cout<<"\nSalary:
"<<Records.salary;
cout<<"\n";
}
}
int last_rec=seqfile.tellg();//last record position
//formula
for computing total number of objects in the file
n=last_rec/(sizeof(Rec));
cout<<"\n\n Total number of objects are
"<<n<<"(considering logical deletion)";
seqfile.close();
}
void EMP_CLASS::Update()
{
int pos;
cout<<"\n
For updation,";
fstream seqfile;
seqfile.open("EMP.DAT",ios::in
| ios::out | ios::binary);
seqfile.seekg(0,ios::beg);
//obtaining
the position of desired record in the file
pos=Search();
if(pos= = ‒1)
{
cout<<"\n
The record is not present in the file";
retum;
}
//calculate
the actual offset of the desired record in the file
int offset=pos* sizeof(Rec);
I/O Systems and File I/O
seqfile.seekp(offset);//seeking
the desired record for modification

//New values for the updation of record
cout<<"\n
Enter the values for updation...";
cout<<"\n
Name: ";cin>>Records.name;
cout<<"\n
Emp_Id: ";cin>>Records.emp_id;
cout<<"\n
Salary: "; cin>>Records.salary;
seqfile.write((char*)&Records,sizeof(Records))<<flush;
seqfile.seekg(0);
seqfile.close();
cout<<"\n The record is updated!!!";
}
void EMP_CLASS::Delete()
{
int id,pos;
cout<<"\n For deletion,";
fstream seqfile;
seqfile.open("EMP.DAT", ios::in | ios::out |
ios::binary);
seqfile.seekg(0,ios::beg);//seeking
for reading purpose
pos=Search();//finding
pos. for the record to be deleted
if(pos= = ‒1)
{
cout<<"\n The record is not present in the
file";
return;
}
//calculate
offset to locate the desired record in the file
int offset=pos* sizeof(Rec);
seqfile.seekp(offset);//seeking the desired record for deletion
strcpy(Records.name,"");
Records.emp_id=
‒1;
Records.salary=
‒1;
seqfile.write((char*)&Records,sizeof(Records))<<flush;
seqfile.seekg(0);
seqfile.close();
cout<<"\n The record is Deleted!!!";
}
void EMP_CLASS::Append()
{
fstream seqfile;
seqfile.open("EMP.DAT", ios::ate ios::in | ios::out |
ios::binary);
seqfile.seekg(0,ios::beg);
int i=0;
while(seqfile.read((char *)&Records,sizeof(Records)))
{
i++;//going through all the records
// for reaching at the end of the
file
}
//instead of above while loop
//we can also use seqfile.seekg(0,ios::end)
//for reaching at the end of the file
seqfile.clear();//turning
off EOF flag
cout<<"\n Enter the record for appending";
cout<<"\nName: "; cin>>Records.name;
cout<<"\nEmp_ID: ";cin>>Records.emp_id;
cout<<"\nSalary: "; cin>>Records.salary;
seqfile.write((char*)&Records,sizeof(Records)); and
seqfile.seekg(0);//reposition
to start(optional)
seqfile.close();
cout<<"\n The record is Appended!!!";
}
int EMP_CLASS::Search()
{
fstream seqfile;
int id,pos;
cout<<"\n Enter the Emp_ID for searching the record
";
cin>>id;
seqfile.open("EMP.DAT",ios::ate | ios::in | ios::out |
ios::binary);
seqfile.seekg(0,ios::beg);
pos=‒1;
int i=0;
while(seqfile.read((char *)&Records,sizeof(Records)))
{
if(id==Records.emp_id)
{
pos=i;
break;
}
i++;
}
return pos;
}
void main()
{
EMP CLASS List;
char ans='y';
int choice,key;
clrscr();
do
{
cout<<"\n
Main Menu "<<endl;
cout<<"\n 1.Create";
cout<<"\n 2.Display";
cout<<"\n 3.Update";
cout<<"\n 4.Delete";
cout<<"\n 5.Append";
cout<<"\n 6.Search";
cout<<"\n 7.Exit";
cout<<"\n Enter your choice ";
cin>>choice;
switch(choice)
{
case 1:List.Create();
break;
case 2:List.Display();
break;
case 3:List.Update();
break;
case 4:List.Delete();
break;
case 5:List.Append();
break;
case 6:key=List.Search();
if(key<0)
cout<<<<<<"\n
Record is not present in the file";
else
cout<<"\n
Record is present in the file";
break;
case 7:exit(0);
}
cout<<"\n\t Do you want to go back to Main
Menu?";
cin>>ans;
}while(ans=='y');
}
Main Menu
1.Create
2.Display
3.Update
4.Delete
5.Append
6.Search
7.Exit
Enter your choice 1
Enter Name: AAA
Enter Emp_ID: 10
Enter Salary: 1000
Do you want to add
more records?y
Enter Name: BBB
Enter Emp ID: 20
Enter Salary: 2000
Do you want to add
more records?y
Enter Name: CCC
Enter Emp_ID: 30
Enter Salary: 3000
Do you want to add
more records?n
Do you want to go back
to Main Menu?y
Main Menu
1.Create
2.Display
3.Update
4.Delete
5.Append
6.Search
7.Exit
Enter your choice 2
The Contents of file
are …
Name: AAA
Emp_ID: 10
Salary: 1000
Name: BBB
Emp ID: 20
Salary: 2000
Name: CCC
Emp_ID: 30
Salary: 3000
Total number of
objects are 3(considering logical deletion)
Do you want to go back
to Main Menu?y
Main Menu
1.Create
2.Display
3.Update
4.Delete
5.Append
6.Search
7.Exit
Enter your choice 3
For updation,
Enter the Emp_ID for
searching the record 20
Enter the values for
updation...
Name: XXX
Emp_Id: 30
Salary: 10000
The record is
updated!!!
Do you want to go back
to Main Menu?n
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main() {
ifstream inFile("myfile.txt");
string line;
if(!inFile) {
cout<<"Error
opening File!!!"<<endl;
return 1;
}
cout<<"Reading file as follows ..."<<endl;
while(getline(inFile,line)){
cout<<line<<endl;
}
inFile.close();
return 0;
}
Reading file as
follows...
This is first line
This is second line
This is the last line
Object Oriented Programming: Chapter 6: IO Systems and File IO : Tag: Oops, Computer Programming : C++ Object Oriented Programming - Sequential Read and Write Operations
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
English Essentials II
EN25C02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Tamils and Technology தமிழர்களும் தொழில்நுட்பமும்
UC25H02 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Linear Algebra
MA25C02 2nd Semester | 2025 Regulation
Applied Physics (CSIE) II
PH25C03 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Digital Principles and Computer Organization
CS25C06 2nd Semester AIDS, CSE, IT, CSE(CY) Dept | 2025 Regulation | 2nd Semester 2025 Regulation
Basic Electrical and Electronics Engineering
EE25C01 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs)
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation
Re-Engineering for Innovation
ME25C05 2nd Semester | 2025 Regulation | 2nd Semester 2025 Regulation
Object Oriented Programming (OOPs) - Laboratory
CS25C07 2nd Semester CSE, CSE(CY) Depts | 2025 Regulation | 2nd Semester 2025 Regulation