Read a specific line from a file: Difference between revisions
Content deleted Content added
Add Seed7 example |
|||
Line 190: | Line 190: | ||
puts [getNthLineFromFile example.txt 7]</lang> |
puts [getNthLineFromFile example.txt 7]</lang> |
||
Where it is necessary to provide very fast access to lines of text, it becomes sensible to create an index file describing the locations of the starts of lines so that the reader code can <code>seek</code> directly to the right location. This is rarely needed, but can occasionally be helpful. |
Where it is necessary to provide very fast access to lines of text, it becomes sensible to create an index file describing the locations of the starts of lines so that the reader code can <code>seek</code> directly to the right location. This is rarely needed, but can occasionally be helpful. |
||
=={{header|TUSCRIPT}}== |
|||
<lang tuscript> |
|||
$$ MODE TUSCRIPT |
|||
file="lines.txt" |
|||
ERROR/STOP OPEN (file,READ,-std-) |
|||
line2fetch=7 |
|||
--> solution 1 |
|||
ACCESS file: READ/RECORDS/UTF8 $file s,line |
|||
LOOP n=1,99 |
|||
READ/NEXT/EXIT file |
|||
IF (n==line2fetch) PRINT line |
|||
ENDLOOP |
|||
ENDACCESS file |
|||
--> solution 2 |
|||
line1to7=FILE (file,#line2fetch) |
|||
line=SELECT (line1to7,#line2fetch) |
|||
PRINT line |
|||
</lang> |
|||
Output: |
|||
<pre> |
|||
line7 |
|||
line7 |
|||
</pre> |
|||
[[Category:File handling]] |
[[Category:File handling]] |