C++ ofstream origin values during file access

Introduction

DescriptionoriginEquivalent
Beginning of file SEEK_SET ios::beg
Current file position SEEK_CUR ios::cur
End of fileSEEK_END ios::end

The origins SEEK_SET, SEEK_CUR, and SEEK_END are defined in stdio.h.

The equivalents ios::beg, ios::cur, and ios::end are defined in fstream.h.

#include <fstream.h>
#include <stdlib.h>
#include <stdio.h>
ifstream in_file;   // Input file pointer.
ofstream scrn;      // Screen pointer.
ofstream prnt;      // Printer pointer.
void main()/*  ww  w .  ja  v  a  2 s  .  c om*/
{
   char in_char;
   in_file.open("MYFILE.TXT", ios::in);
   if (!in_file)
   {
      cout << "\n*** Error opening MYFILE.TXT ***\n";
      exit(0);
   }
   scrn.open("CON", ios::out);    // Open screen device.
   while (in_file.get(in_char))
   {
      scrn << in_char;
   } // Output characters to the screen.
   scrn.close();  // Close screen because it is no longer needed.
   in_file.seekg(0L, SEEK_SET);   // Reposition file pointer.
   prnt.open("LPT1", ios::out);   // Open printer device.
   while (in_file.get(in_char))
   {
      prnt << in_char;
   }

   prnt.close();          // Always close all open files.
   in_file.close();
   return;
}



PreviousNext

Related