Task: Read a specific line from a file


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

int main( ) {
   std::cout << "Which file do you want to look at ?\n" ;
   std::string input ;
   std::getline( std::cin , input ) ;
   std::ifstream infile( input.c_str( ) , std::ios::in ) ;
   std::string file( input ) ;
   std::cout << "Which file line do you want to see ? ( Give a number > 0 ) ?\n" ;
   std::getline( std::cin , input ) ;
   int linenumber = std::stoi( input ) ;
   int lines_read = 0 ;
   std::string line ;
   if ( infile.is_open( ) ) {
      while ( infile ) {
	 getline( infile , line ) ;
	 lines_read++ ;
	 if ( lines_read == linenumber ) {
	    std::cout << line << std::endl ;
	    break ; 
	 }
      }
      infile.close( ) ;
      if ( lines_read < linenumber ) 
	 std::cout << "No " << linenumber << " lines in " << file << " !\n" ;
      return 0 ;
   }
   else {
      std::cerr << "Could not find file " << file << " !\n" ;
      return 1 ;
   }
}









Fortran:       MODULE SAMPLER    
       CONTAINS                                                         SAM00200
        CHARACTER*20 FUNCTION GETREC(N,F,IS)    
Careful. Some compilers get confused over the function name's usage.    SAM00400
         INTEGER N              
         INTEGER F              
         CHARACTER*(*) IS       
         INTEGER I,L            
          IS = ""               
          IF (N.LE.0) THEN      
            WRITE (GETREC,1) "
    1       FORMAT (A,1X,I0)                    
          ELSE IF (F.LE.0) THEN                 
            WRITE (GETREC,1) "
          ELSE IF (LEN(IS).LE.0) THEN           
            WRITE (GETREC,1) "
          ELSE                  
            REWIND (F)          
            DO I = 1,N - 1      
              READ (F,2,END=3)  
            END DO              
            READ (F,2,END = 3) L,IS(1:MIN(L,LEN(IS)))    
    2       FORMAT (Q,A)        
            IF (L.LT.LEN(IS)) IS(L + 1:) = ""   
            IF (L.GT.LEN(IS)) THEN              
              WRITE (GETREC,1) "+Length",L      
            ELSE IF (L.LE.0) THEN               
              WRITE (GETREC,1) "+Null"          
            ELSE IF (IS.EQ."") THEN             
              WRITE (GETREC,1) "+Blank",L       
            ELSE                                
              WRITE (GETREC,1) " Length",L      
            END IF              
          END IF                
          RETURN                
    3     WRITE (GETREC,1) "
        END FUNCTION GETREC     
      END MODULE SAMPLER        
                                                                        SAM03900
      PROGRAM POKE                                                      POK00100
      USE SAMPLER                                                       POK00200
      INTEGER ENUFF     
      PARAMETER (ENUFF = 666)   
      CHARACTER*(ENUFF) STUFF   
      CHARACTER*20 RESULT                                               POK00600
      INTEGER MSG,F     
      MSG = 6           
      F = 10            
      WRITE (MSG,*) "      To select record 7 from a disc file."        POK01000
                                                                        POK01100
      WRITE (MSG,*) "As a FORMATTED file."                              POK01200
      OPEN (F,FILE="FileSlurpN.for",STATUS="OLD",ACTION="READ")         POK01300
      RESULT = GETREC(7,F,STUFF)                                        POK01400
      WRITE (MSG,1) "Result",RESULT                                     POK01500
      WRITE (MSG,1) "Record",STUFF                                      POK01600
    1 FORMAT (A,":",A)                                                  POK01700
                                                                        POK01800
      CLOSE (F)                                                         POK01900
      WRITE (MSG,*) "As a random-access unformatted file."              POK02000
      OPEN (F,FILE="FileSlurpN.for",STATUS="OLD",ACTION="READ",         POK02100
     1 ACCESS="DIRECT",FORM="UNFORMATTED",RECL=82)      
      STUFF = "Cleared."                                                POK02300
      READ (F,REC = 7,ERR = 666) STUFF(1:80)                            POK02400
      WRITE (MSG,1) "Record",STUFF(1:80)                                POK02500
      STOP                                                              POK02600
  666 WRITE (MSG,*) "Can't get the record
      END