Task: Read a specific line from a file








VB: Function read_line(filepath,n)
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objFile = objFSO.OpenTextFile(filepath,1)
	arrLines = Split(objFile.ReadAll,vbCrLf)
	If UBound(arrLines) >= n-1 Then
		If arrLines(n-1) <> "" Then
			read_line = arrLines(n-1)
		Else
			read_line = "Line " & n & " is null."
		End If
	Else
		read_line = "Line " & n & " does not exist."
	End If
	objFile.Close
	Set objFSO = Nothing
End Function

WScript.Echo read_line("c:\temp\input.txt",7)



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