Read a file line by line: Difference between revisions
Content added Content deleted
m (Minor spacing fix) |
m (→{{header|REXX}}: elided hardcoded input filename versions, elided a blank line.) |
||
Line 2,699: | Line 2,699: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===basic version=== |
|||
<lang REXX>/*REXX program to read and display (with count) a file, one line at a time.*/ |
|||
parse arg fileID . |
|||
say 'Displaying file:' fileID |
|||
do linenumber=1 while lines(fileID)\==0 /* loop construct */ |
|||
line=linein(fileID) /* read line */ |
|||
say 'Line' linenumber':' line /* show line number and line */ |
|||
end linenumber /* end loop and confirm which loop */ |
|||
say |
|||
say 'File' fileID 'has' linenumber-1 'lines.' /*summary.*/</lang> |
|||
===known name version=== |
|||
Or: the 'known name' short version: |
|||
<lang rexx>file='foobar.txt' |
|||
do while lines(file)\==0; say linein(file); end</lang> |
|||
===belt and suspenders=== |
===belt and suspenders=== |
||
The first '''linein''' invocation is used to position the record pointer (current position in the file for reading) |
The first '''linein''' invocation is used to position the record pointer (current position in the file for reading) |
||
Line 2,735: | Line 2,717: | ||
say 'file ' fID " has " #-1 ' records.' /*display the record count summary. */ |
say 'file ' fID " has " #-1 ' records.' /*display the record count summary. */ |
||
call lineout fID /*close the input file (most REXXes). */ |
call lineout fID /*close the input file (most REXXes). */ |
||
/*stick a fork in it, we're all done. */</lang> |
/*stick a fork in it, we're all done. */</lang><br><br> |
||
<br><br> |
|||
===ARexx version=== |
===ARexx version=== |
||
<lang rexx>/* Also works with Regina if you state OPTIONS AREXX_BIFS ; OPTIONS AREXX_SEMANTICS */ |
<lang rexx>/* Also works with Regina if you state OPTIONS AREXX_BIFS ; OPTIONS AREXX_SEMANTICS */ |
||
Line 2,750: | Line 2,730: | ||
ELSE EXIT 20 |
ELSE EXIT 20 |
||
CALL Close filehandle |
CALL Close filehandle |
||
EXIT 0 |
EXIT 0</lang> |
||
</lang> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |