Skip to content

Instantly share code, notes, and snippets.

@Zulqurnain
Created September 18, 2013 15:31
Show Gist options
  • Save Zulqurnain/6610946 to your computer and use it in GitHub Desktop.
Save Zulqurnain/6610946 to your computer and use it in GitHub Desktop.
Seekg() , Tellg() , Seekp() , Seekg()
#include <iostream.h>
#include <fstream.h>
int main()
{
ofstream fout;
int i; char ch;
// File opening in output mode both
fout.open("d:/test.txt",ios::in);
if(!fout)
{
cout<<"Error in creating file..\n"; return 0;
}
// Writting A-Z in file.
for(i=0;i<26;i++){
fout.put((char)(65+i));
}
cout<<"Total bytes are : "<< fout.tellp() << endl;
fout.close();
// reading file ..
ifstream fin;
fin.open("d:/test.txt",ios::in);
if(!fin)
{
cout<<"Error in opening file..\n"; return 0;
}
cout<<"\nCASE-1:\nFile's get pointer is in " << fin.tellg() <<" Position. \n";
cout<<"Characters are ... :\n";
while(!fin.eof()){
fin.get(ch);
cout<< ch;
}
// sets the get pointer to 10th byte
fin.clear(); // reset flags
fin.seekg(10,ios::beg);
cout<<"\n\nCASE-2:\nFile's get pointer is in " << fin.tellg() <<" Position. \n";
cout<<"Characters are ... :\n";
while(!fin.eof()){
fin.get(ch);
cout<< ch;
}
// sets the get pointer to -20 bytes back
fin.clear(); // reset flags
fin.seekg(-20,ios::cur);
cout<<"\n\nCASE-2:\nFile's get pointer is in " << fin.tellg() <<" Position. \n";
cout<<"Characters are ... :\n";
while(!fin.eof()){
fin.get(ch);
cout<< ch;
}
cout<< endl;
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment