File Pointers
File Pointers
com
seekg():-
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():-
tellg() is used to know where the get pointer is in a file.
Syntax: file_pointer.tellg();
Example: int posn = fin.tellg();
seekp():-
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():-
tellp() is used to know where the put pointer is in a file.
Syntax: file_pointer.tellp();
Example: int posn=fout.tellp();
1|Page
Ashishprajapati29.wordpress.com
Program:
#include<iostream>
#include<fstream>
int main()
fstream fout;
char buf[200];
fout.open("file1.txt",ios::trunc | ios::out);
char ch[100];
cin.getline(ch,20,'#');
fout<<ch;
int pos;
pos = fout.tellp();/*..........................................*///////tellp
fout.seekp(3,ios::beg); /*..........................................*///////seekp
//////seekp
fout<<"nkok";
2|Page
Ashishprajapati29.wordpress.com
fout.close();
cout << "\nReading from the file ... " << endl;
fout.seekg(3); /*..........................................*///////seekg
while (!fout.eof()) {
fout.getline(buf, 100);
Output:
3|Page