Read a specific line from a file: Difference between revisions

m
(add julia example)
Line 566:
 
=={{header|Julia}}==
ThisThe short following snippet of code actually stores all the lines infrom the file in an array and displaydisplays the seventh element of the array, returning an error if there is no such element. Since the array is not referenced, it will be garbage collected when needed. The filehandle is closed upon completion of the task, be it successful or not.
<lang Julia>open(readlines, "path/to/file")[7]</julialang>
The next function readreads n lines in the file and displaydisplays the last read if possible, or returnreturns a short message. Here again, the filehandle is automatically closed after the task. Note that the first line is returned if a negative number is given as the line number.
<lang Julia>function read_nth_lines (stream, num)
Here again, the filehandle is automatically closed after the task.
<lang Julia>function read_nth_lines (stream, num)
for i = 1:num-1
readline(stream)