File Pointers and Random Access Functions
In C++ random access is achieved by manipulating following functions
seekg():
This function helps us to take the get pointer to a specific location in the file.
seekp():
This function helps us to take the put pointer to a specific location in the file.
tellg():
This function helps us to find the position of get pointer in the file.(distance of get pointer from the beginning of the file in terms of bytes)
tellp():
This function helps us to find the position of put pointer in the file.( distance of put pointer from the beginning of the file in terms of bytes)
seekg() and tellg() functions are for input stream (ifstream) and seekp() and tellp() functions are for output stream (ofstream).
Note:
However if we use them with an fstream object then tellg() and tellp() work in the same manner and seekg() and seekp() work in the same manner.
Example of using seekg() function:
Syntax:
ifstream abc;
abc.open(“filename”);
abc.seekg(no_of_bytes,position);
Where:
istream: inbuilt class to create an object
abc: user-defined object to open the file in input mode i.e. for reading
filename: user-defined file name which stores the data
seekg(): this function takes the get pointer to a specific location
no_of_bytes: can be 0(zero), positive or negative
position: can be any of the following
ios::beg => from beginning of the file
ios::cur => from current position
ios::end => from end of the file
Example:1
abc.seekg(10,ios::beg);
Takes file get pointer 10 bytes ahead from beginning of the file.
Example:2
abc.seekg(0,ios::beg);
Takes file get pointer to the beginning of the file.
Example:3
abc.seekg(10);
Takes file get pointer 10 bytes ahead from beginning of the file.(if position is not mentioned by default it takes ios::beg)
Example:4
abc.seekg(10,ios::cur);
Takes file get pointer 10 bytes ahead from current position in the file.
Example:5
abc.seekg(-10,ios::cur);
Takes the file get pointer 10 bytes backward from current position in the file.
Example:6
abc.seekg(0,ios::end);
Takes file get pointer to the end of the file.(zero bytes from end)
Example:7
abc.seekg(-10,ios::end);
take the file get pointer 10 bytes backward from the end of the file.
Example of using seekp() function:
Syntax:
ifstream abc;
abc.open(“hello”);
abc.seekp(no_of_bytes,position);
Where:
istream: inbuilt class to create an object
abc: user-defined object to open the file in input mode i.e. for reading
filename: user-defined file name which stores the data
seekp(): this function takes the put pointer to a specific location
no_of_bytes: can be 0(zero), positive or negative
position: can be any of the following
ios::beg => from beginning of the file
ios::cur => from current position
ios::end => from end of the file
Example:1
abc.seekp(10,ios::beg);
Takes file put pointer 10 bytes ahead from beginning of the file.
Example:2
abc.seekp(0,ios::beg);
Takes file put pointer to the beginning of the file.
Example:3
abc.seekp(10);
Takes file put pointer 10 bytes ahead from beginning of the file.(if position is not mentioned by default it takes ios::beg)
Example:4
abc.seekp(10,ios::cur);
Takes file put pointer 10 bytes ahead from current position in the file.
Example:5
abc.seekp(-10,ios::cur);
Takes the file put pointer 10 bytes backward from current position in the file.
Example:6
abc.seekp(0,ios::end);
Takes file put pointer to the end of the file.(zero bytes from end)
Example:7
abc.seekp(-10,ios::end);
Takes the file put pointer 10 bytes backward from the end of the file.
Example: class student { private: int roll; char name[10]; float age; public: void disp(); void disp_all(); void rand_disp(); }; void student::rand_disp() { fstream infile; student s; long len=0,trec=0,flen=0; int n; infile.open("stud1.dat",ios::in); infile.seekg(0,ios::end); flen=infile.tellp(); cout<<"rec size is "<<sizeof(s)<<endl; len=sizeof(s); trec=flen/len; cout<<"total records are "<<trec<<endl; cout<<"total file length is "<<flen<<endl; cout<<"Enter the rec number to display "; cin>>n; if (n<=0 || n>trec) { cout<<"Invalid record number "; getch(); } else { infile.seekg(0,ios::beg); infile.seekg((n-1)*sizeof(s),ios::beg); infile.read((char*)&s,sizeof(s)); cout<<"roll "<<s.roll<<endl; cout<<"name is " <<s.name<<endl; cout<<"age "<<s.age<<endl; } }