Read a file line by line: Difference between revisions

→‎{{header|REXX}}: simplified the first REXX version, added a version that shows the length along with the record number, and it can also only show specific line numbers.
m (→‎belt and suspenders: moved a comment.)
(→‎{{header|REXX}}: simplified the first REXX version, added a version that shows the length along with the record number, and it can also only show specific line numbers.)
Line 2,705:
<br><br>The &nbsp; '''lineout''' &nbsp; BIF closes the file (in most REXX interpreters); &nbsp; this is done for general housekeeping.
<lang rexx>/*REXX program reads and displays (with a count) a file, one line at a time. */
parse arg fID .; if fID=='' then exit /*Wasobtain nooptional fileIDargument specified?from the Then quitCL. */
if fID=='' then exit 8 /*Was no fileID specified? Then quit. */
say center(' displaying file: ' fID" ", 79, '═') /*show the name of the file being read.*/
call linein fID, 1, 0 /*see the comment in the section header*/
say /* [↓] show a file's contents (lines).*/
do #=1 while lines(fID)\==0; y=linein(fID) /*loop whilst there are lines in file. */
say y= linein(fID) /*showread a blank line forand peruseabilityassign contents to Y. */
say 'record#=' # " length=" length(y) /*show the record number and the length*/
say y /*show the content of the line (record)*/
end /*#*/
say /*stick a fork in it, we're all done. */
say center(' file ' fID " has " #-1 ' records.', 79, /*display'═') the record count summary. /*show rec count. */
call lineout fID /*close the input file (most REXXes). */</lang><br><br>
 
===ARexx deluxe version ===
This REXX version, in addition to the first version (above), &nbsp; has the ability to show the record number and length as the
file's contents are being displayed.
<br>It also has the ability to only show a specific line (or a group of lines).
<br>It can also just show the last line.
<br>If appropriate, the program will show the total number of lines in the file.
<lang rexx></lang>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 123456.TXT </tt>}}
<pre>
════════════════════════ displaying file: 123456.TXT ═════════════════════════
 
11 22222222 33333333 444 5555555555 6666666
111 2222222222 3333333333 4444 5555555555 666666666
1111 22 22 33 33 44 44 55 66 66
11 22 22 33 44 44 55 66
11 22 33 44 44 55 66
11 22 333 4444444444 5555555 66 666666
11 22 333 4444444444 55555555 6666666666
11 22 33 44 55 666 66
11 22 33 44 55 66 66
11 22 22 33 33 44 55 55 66 66
111111 2222222222 3333333333 4444 5555555555 66666666
111111 2222222222 33333333 4444 55555555 666666
 
─────────────────────────────────────────── file 123456.TXT has 12 records.
</pre>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 123456.TXT &nbsp; -1 </tt>}}
<pre>
════════════════════════ displaying file: 123456.TXT ═════════════════════════
 
─────────────────────────────────────────────────────── record#= 1 length= 80
11 22222222 33333333 444 5555555555 6666666
 
─────────────────────────────────────────────────────── record#= 2 length= 81
111 2222222222 3333333333 4444 5555555555 666666666
 
─────────────────────────────────────────────────────── record#= 3 length= 81
1111 22 22 33 33 44 44 55 66 66
 
─────────────────────────────────────────────────────── record#= 4 length= 73
11 22 22 33 44 44 55 66
 
─────────────────────────────────────────────────────── record#= 5 length= 73
11 22 33 44 44 55 66
 
─────────────────────────────────────────────────────── record#= 6 length= 80
11 22 333 4444444444 5555555 66 666666
 
─────────────────────────────────────────────────────── record#= 7 length= 81
11 22 333 4444444444 55555555 6666666666
 
─────────────────────────────────────────────────────── record#= 8 length= 81
11 22 33 44 55 666 66
 
─────────────────────────────────────────────────────── record#= 9 length= 81
11 22 33 44 55 66 66
 
────────────────────────────────────────────────────── record#= 10 length= 81
11 22 22 33 33 44 55 55 66 66
 
────────────────────────────────────────────────────── record#= 11 length= 80
111111 2222222222 3333333333 4444 5555555555 66666666
 
────────────────────────────────────────────────────── record#= 12 length= 79
111111 2222222222 33333333 4444 55555555 666666
 
 
─────────────────────────────────────────── file 123456.TXT has 12 records.
</pre>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 123456.TXT &nbsp; 5 &nbsp; 6 </tt>}}
<pre>
═════════════════════════ displaying file: 1234.txt ══════════════════════════
 
11 22 33 44 44
11 22 333 4444444444
</pre>
{{out|output|text=&nbsp; when using the input of: &nbsp; &nbsp; <tt> 123456.TXT &nbsp; 5 &nbsp; 6 </tt>}}
════════════════════════ displaying file: 123456.TXT ═════════════════════════
 
 
───────────────────────────────────────── record#= 12 (last line) length= 79
111111 2222222222 33333333 4444 55555555 666666
─────────────────────────────────────────── file 123456.TXT has 12 records.
 
=== ARexx version ===
<lang rexx>/* Also works with Regina if you state OPTIONS AREXX_BIFS ; OPTIONS AREXX_SEMANTICS */
filename='file.txt'