www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/09/24/11:22:50

Message-ID: <3BAF4C56.3F2904BC@erols.com>
Date: Mon, 24 Sep 2001 11:08:06 -0400
From: Chris Smith <herrsmitty AT erols DOT com>
X-Mailer: Mozilla 4.72 [en]C-CCK-MCD DT (Win98; U)
X-Accept-Language: en
MIME-Version: 1.0
To: djgpp AT delorie DOT com
Subject: read a file backwards
Reply-To: djgpp AT delorie DOT com

Hello everyone,
I am working on a small encryption utility program. At one point in the
program I need to be able to read a file backwards. Have I missed
something specific about djgpp, or have I just over looked the obvious?
Below is a sample of the code I am attempting to use:

#include<fstream>

int main(){

 // pick a file to open
 char file[] = "somefile.txt";

 // open the file stream in binary mode
 ifstream SOURCE(file,ios::binary);
 if(!SOURCE){exit(1);}

 // set up a variable to hold what we read
 char current = '\0';

 // goto the end of the file
 SOURCE.seekg( 0 , ios::end );

 // get the end of the file, -1 for 0-based counting
 int TheEnd = -1 * (SOURCE.tellg() - 1);

 // loop through the file
 for( int i  =  -1 ;  i  >  TheEnd ;  i-- ){
    SOURCE.get(current);
    cout<<current;
    SOURCE.seekg( i , ios::end );
 }

 SOURCE.close();
 return 0;
}

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019