While performing file operations, we must be able to reach at any desired position inside the file. For this purpose there are two commonly used functions:
Accessing
Records Randomly
•
While performing file operations, we
must be able to reach at any desired position inside the file. For this purpose
there are two commonly used functions‒
The
seek operation is using two functions seekg and seekp.
• seekg
‒ means get pointer of specific location for reading of record.
• seekp
‒ means get pointer of specific location for writing of record.
The
syntax of seek is
seekg
(offset, reference ‒ position);
seekp
(offset, reference ‒ position);
where,
offset is any constant specifying
the location.
reference ‒ position is
for specifying beginning, end or current position. It can be specified as,
ios
:: beg for beginning location
ios:
end for end of file
ios:
cur for current location
This
function tells us the current position.
For example
seqfile.
tellg () ‒ gives current position of get pointer (for
reading the record).
seqfile.
tellp () ‒ gives current position of put pointer (for
writing the record).
/************************************************************************
Program for demonstrating random access to the file
************************************************************************/
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
int main()
{
ifstream in;
char Data[80];
in.open("Sample.dat");
if (lin)
{
// Print an error
and exit
cerr <<
"Sample.dat could not be opened for reading!" << endl;
exit(1);
}
cout<<"First statement of the file
..."<<<<endl;
in.seekg(0,ios::beg);
// move to the beginning character
// Get the rest of the line and print it
in.getline(Data,80);
cout <<<"\t"<<Data << endl;
cout<<"The
current position is: "<<in.tellg()<<endl;
cout<<"Moving to 3rd statement of the file
..."<<endl;
in.seekg(13, ios::cur);
// Get rest of the line and print it
in.getline(Data,80);
cout <<"\t"<<Data << endl;
cout<<"Moving to 6th statement of the file
..."<<endl;
in.seekg(26,
ios::cur);
// Get rest of the line and print it
in.getline(Data,80);
cout<<"\t"<<Data << endl;
cout<<"Moving to last statement of the file
..."<<endl;
in.seekg(‒14,
ios::end);
// Get rest of the line and print it
in.getline(Data,80);
cout <<"\t"<<Data << endl;
in.close();
return 0;
}
<Sample.dat>
Statement 1
Statement 2
Statement 3
Statement 4
Statement 5
Statement 6
Statement 7
Statement 8
Statement 9
Statement 10
First statement of the
file ...
Statement 1
The current position
is: 13
Moving to 3rd
statement of the file ...
Statement 3
Moving to 6th
statement of the file ...
Statement 6
Moving to last
statement of the file ...
Statement 10
Object Oriented Programming: Chapter 6: IO Systems and File IO : Tag: Oops, Computer Programming : C++ Object Oriented Programming - Accessing Records Randomly
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