Task: Remove lines from a file


C++: #include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
#include <list>

void deleteLines( const std::string & , int , int ) ;

int main( int argc, char * argv[ ] ) {
   if ( argc != 4 ) {
      std::cerr << "Error! Invoke with <deletelines filename startline skipnumber>!\n" ;
      return 1 ;
   }
   std::string filename( argv[ 1 ] ) ;
   int startfrom = atoi( argv[ 2 ] ) ;
   int howmany = atoi( argv[ 3 ] ) ;
   deleteLines ( filename , startfrom , howmany ) ;
   return 0 ;
}

void deleteLines( const std::string & filename , int start , int skip ) {
   std::ifstream infile( filename.c_str( ) , std::ios::in ) ;
   if ( infile.is_open( ) ) {
      std::string line ;
      std::list<std::string> filelines ;
      while ( infile ) {
	 getline( infile , line ) ;
	 filelines.push_back( line ) ;
      }
      infile.close( ) ;
      if ( start > filelines.size( ) ) {
	 std::cerr << "Error! Starting to delete lines past the end of the file!\n" ;
	 return ;
      }
      if ( ( start + skip ) > filelines.size( ) ) {
	 std::cerr << "Error! Trying to delete lines past the end of the file!\n" ;
	 return ;
      }
      std::list<std::string>::iterator deletebegin = filelines.begin( ) , deleteend ;
      for ( int i = 1 ; i < start ; i++ )
	 deletebegin++ ;
      deleteend = deletebegin ;
      for( int i = 0 ; i < skip ; i++ )
	 deleteend++ ;
      filelines.erase( deletebegin , deleteend ) ;
      std::ofstream outfile( filename.c_str( ) , std::ios::out | std::ios::trunc ) ;
      if ( outfile.is_open( ) ) {
	 for ( std::list<std::string>::const_iterator sli = filelines.begin( ) ;
	       sli != filelines.end( ) ; sli++ )
	    outfile << *sli << "\n" ;
      }
      outfile.close( ) ;
   }
   else {
      std::cerr << "Error! Could not find file " << filename << " !\n" ;
      return ;
   }
}









Fortran:       SUBROUTINE CROAK(GASP)	
       CHARACTER*(*) GASP	
        WRITE (6,*) "Oh dear. ",GASP	
        STOP "++ungood."	
      END			

      SUBROUTINE FILEHACK(FNAME,IST,N)
       CHARACTER*(*) FNAME	
       INTEGER IST		
       INTEGER N		
       INTEGER ENUFF,L		
       PARAMETER (ENUFF = 66666)
       CHARACTER*(ENUFF) ALINE	
       INTEGER NREC		
       INTEGER F,T		
       PARAMETER (F=66,T=67)	
       LOGICAL EXIST
        IF (FNAME.EQ."") CALL CROAK("Blank file name
        IF (IST.LE.0)    CALL CROAK("First record must be positive
        IF (N.LE.0)      CALL CROAK("Remove count must be positive
        INQUIRE(FILE = FNAME, EXIST = EXIST)	
        IF (.NOT.EXIST) CALL CROAK("Can't find a file called "//FNAME)	
        OPEN (F,FILE=FNAME,STATUS="OLD",ACTION="READ",FORM="FORMATTED")	
        OPEN (T,STATUS="SCRATCH",FORM="FORMATTED")	
        NREC = 0		
Copy the desired records to a temporary file.
   10   READ (F,11,END = 20) L,ALINE(1:MIN(L,ENUFF))	
   11   FORMAT (Q,A)		
        IF (L.GT.ENUFF) CALL CROAK("Ow
        NREC = NREC + 1		
        IF (NREC.LT.IST .OR. NREC.GE.IST + N) WRITE (T,12) ALINE(1:L)	
   12   FORMAT (A)		
        GO TO 10		
Convert from input to output...
   20   IF (NREC.LT.IST + N) CALL CROAK("Insufficient records
        REWIND T		
        CLOSE(F)		
        OPEN (F,FILE=FNAME,FORM="FORMATTED",	
     1   ACTION="WRITE",STATUS="REPLACE")	
Copy from the temporary file.
   21   READ (T,11,END = 30) L,ALINE(1:L)	
        WRITE (F,12) ALINE(1:L)			
        GO TO 21		
Completed.
   30   CLOSE(T)		
        CLOSE(F)		
      END		

      PROGRAM CHOPPER
       CALL FILEHACK("foobar.txt",1,2)
      END