C++ fstream seekg()

Description

C++ fstream seekg()

#include <fstream.h>
#include <stdlib.h>
#include <stdio.h>
fstream fp;//from   w w  w.j a v a 2 s  . com
void main()
{
   char ch;    // Holds A through Z.
   // Open in update mode so you can read file after writing to it.
   fp.open("alph.txt", ios::in | ios::out);
   if (!fp)
   {
      cout << "\n*** Error opening file ***\n";
      exit(0);
   }
   for (ch = 'A'; ch <= 'Z'; ch++)
   {
      fp << ch;
   }  // Write letters.
   fp.seekg(8L, ios::beg);   // Skip eight letters, point to I.
   fp >> ch;
   cout << "The first character is " << ch << "\n";
   fp.seekg(16L, ios::beg);   // Skip 16 letters, point to Q.
   fp >> ch;
   cout << "The second character is " << ch << "\n";
   fp.close();
   return;
}



PreviousNext

Related