Reads the TXT file backwards, printing each character as it skips back in the file. - C++ File Stream

C++ examples for File Stream:Text File

Description

Reads the TXT file backwards, printing each character as it skips back in the file.

Demo Code

#include <fstream>
#include <iostream>
using namespace std;
#include <stdlib.h>
#include <stdio.h>
ifstream fp;/*from   w  w  w .  j  a  v a 2 s.c  o m*/
void main()
{
   int ctr;   // Steps through the 26 letters in the file.
   char in_char;
   fp.open("ALPH.TXT", ios::in);
   if (!fp)
   {
      cout << "\n*** Error opening file ***\n";
      exit(0);
   }
   fp.seekg(-1L, SEEK_END);   // Point to last byte in the file.
   for (ctr = 0; ctr < 26; ctr++)
   {
      fp >> in_char;
      fp.seekg(-2L, SEEK_CUR);
      cout << in_char;
   }
   fp.close();
   return;
}

Result


Related Tutorials