C++ fstream Stores the alphabet in a file, then reads two letters from it.

Description

C++ fstream Stores the alphabet in a file, then reads two letters from it.

#include <fstream.h>
#include <stdlib.h>
#include <stdio.h>
fstream fp;/*from   ww w  .  j a va 2s.c  o m*/
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