Some languages have special semantics for obtaining a known line number from a file.



Task

Demonstrate how to obtain the contents of a specific line within a file.

For the purpose of this task demonstrate how the contents of the seventh line of a file can be obtained,   and store it in a variable or in memory   (for potential future use within the program if the code were to become embedded).

If the file does not contain seven lines,   or the seventh line is empty,   or too big to be retrieved,   output an appropriate message.

If no special semantics are available for obtaining the required line,   it is permissible to read line by line.

Note that empty lines are considered and should still be counted.

Also note that for functional languages or languages without variables or storage,   it is permissible to output the extracted data to standard output.




' FB 1.05.0 Win64
 
Open "input.txt" For Input As #1
Dim line_ As String
Dim count As Integer = 0
While Not Eof(1)
  Line Input #1, line_  '' read each line  
  count += 1
  If count = 7 Then
    line_ = Trim(line_, Any !" \t")   '' remove any leading or trailing spaces or tabs
    If line_ = "" Then
      Print "The 7th line is empty"
    Else
      Print "The 7th line is : "; line_
    End If
    Exit While
  End If
Wend
If count < 7 Then
  Print "There are only"; count; " lines in the file"
End If
Close #1
Print
Print "Press any key to quit"
Sleep 