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.




BYTE FUNC ReadLine(CHAR ARRAY fname CARD index CHAR ARRAY result)
  CHAR ARRAY line(255)
  CARD curr
  BYTE status,dev=[1]
 
  Close(dev)
  Open(dev,fname,4)
  curr=1 status=1
  WHILE Eof(dev)=0
  DO
    InputSD(dev,line)
    IF curr=index THEN
      SCopy(result,line)
      status=0
      EXIT
    FI
    curr==+1
  OD
  Close(dev)
RETURN (status)
 
PROC Test(CHAR ARRAY fname CARD index)
  CHAR ARRAY result(255)
  BYTE status
 
  PrintF("Reading %U line...%E",index)
  status=ReadLine(fname,index,result)
  IF status=0 THEN
    IF result(0)=0 THEN
      PrintF("%U line is empty.%E%E",index)
    ELSE
      PrintF("%U line is:%E""%S""%E%E",index,result)
    FI
  ELSEIF status=1 THEN
    PrintF("File contains less than %U lines.%E%E",index)
  FI
RETURN
 
PROC Main()
  CHAR ARRAY fname="H6:READ__96.ACT"
 
  PrintF("Reading ""%S""...%E%E",fname)
  Test(fname,7)
  Test(fname,24)
  Test(fname,50)
RETURN