Stores the alphabet in a file, reads two letters from it, and changes those letters to xs. - C++ File Stream

C++ examples for File Stream:Text File

Description

Stores the alphabet in a file, reads two letters from it, and changes those letters to xs.

Demo Code

#include <fstream>
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <stdio.h>
fstream fp;/*from  w  w  w.  ja va 2 s.  co 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, SEEK_SET);  // Skip eight letters, point to I.
   fp >> ch;
   // Change the Q to an x.
   fp.seekg(-1L, SEEK_CUR);
   fp << 'x';
   cout << "The first character is " << ch << "\n";
   fp.seekg(16L, SEEK_SET);   // Skip 16 letters, point to Q.
   fp >> ch;
   cout << "The second character is " << ch << "\n";
   // Change the Q to an x.
   fp.seekg(-1L, SEEK_CUR);
   fp << 'x';
   fp.close();
   return;
}

Result


Related Tutorials