Demonstrate seekg(). : File Read « File « C++






Demonstrate seekg().

 

#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int main(int argc, char *argv[])
{
  char ch;

  if(argc!=3) {
    cout << "Usage: LOCATE <filename> <loc>\n";
    return 1;
  }

  ifstream in(argv[1], ios::in | ios::binary);

  if(!in) {
    cout << "Cannot open input file.\n";
    return 1;
  }

  in.seekg(atoi(argv[2]), ios::beg);

  while(!in.eof()) { 
    in.get(ch);
    cout << ch;
  }

  in.close();

  return 0;
}


           
         
  








Related examples in the same category

1.Write char to a fileWrite char to a file
2.Read file contentRead file content
3.Demonstrate gcount().Demonstrate gcount().
4.Use getline() to read a string that contains spaces.Use getline() to read a string that contains spaces.
5.Demonstrate peek() in ifstreamDemonstrate peek() in ifstream
6.Read formatted data from a file.
7.Write unsigned char to a file and read it back