Read a file line by line: Difference between revisions
Add Standard ML version
Drkameleon (talk | contribs) (added Arturo) |
(Add Standard ML version) |
||
(6 intermediate revisions by 5 users not shown) | |||
Line 700:
</syntaxhighlight>
=={{header|
<syntaxhighlight lang="arturo">loop read.lines "myfile.txt" 'line ->
Line 1,488:
=={{header|Elena}}==
ELENA
<syntaxhighlight lang="elena">import system'io;
import extensions;
Line 1,495:
public program()
{
File.assign
}</syntaxhighlight>
Line 1,695:
=={{header|Forth}}==
<syntaxhighlight lang="forth">
\ length. However, READ-LINE may (but does not have to) read up to
\ two line-terminating characters into the buffer after the line, so
\ the buffer size should always be two larger than the limit given to
\ READ-LINE. Lines that are two long to fit into the buffer will be split,
\ so you can't tell they aren't separate lines.
82 constant max-line
: third ( A b c -- A b c A )
>r over r> swap ;
: read-lines ( fileid -- fileid )
begin pad max-line
read-line throw ( fileid chars-read )
2drop▼
while pad swap ( fileid pad chars-read ) \ string excludes the newline
repeat 2drop ;</syntaxhighlight>▼
repeat
\ Get rid of number of characters read by last call to read-line, which is
\ zero because no charaters were read.
drop
;
s" infile.txt" r/o open-file throw read-lines close-file throw
Given the file
<nowiki>Line 1.
This is some text. It should be longer than the buffer size. That makes it weird, don't you think?
Last line.</nowiki>
the result should be something like this:
<nowiki>$ gforth reading-line-by-line-part-1-variant-2.fs -e bye
Line 1.
This is some text. It should be longer than the buffer size. That makes it weird
, don't you think?
Last line.</nowiki>
An alternative version that opens a named file, allocates a buffer of the requested size, reads and
prints each line, frees the buffer, and closes the file.
<syntaxhighlight lang="forth">: read-lines' (
r/
over 2 + \ Add space for up to two line terminators after the buffer.
allocate throw ( buffer-len wfileid buffer-addr )
-rot 2>r ( buffer-addr )
begin
while
repeat
drop free throw
2r> close-file throw drop ;
4096 s"
=={{header|Fortran}}==
Line 3,263 ⟶ 3,293:
''FileHandle.each{}'' is lazy, allowing us to do this:
<syntaxhighlight lang="ruby">File(__FILE__).open_r.each { |line|
}</syntaxhighlight>
Line 3,269 ⟶ 3,299:
<syntaxhighlight lang="ruby">var fh = File(__FILE__).open_r
while (fh.readline(\var line)) {
}</syntaxhighlight>
Line 3,319 ⟶ 3,349:
#.output(#.readline(f))
<</syntaxhighlight>
=={{header|Standard ML}}==
Gets the lines of a file as a list of strings with trailing newline removed.
<syntaxhighlight lang="sml">fun readLines string =
let
val strm = TextIO.openIn path
fun chomp str =
let
val xstr = String.explode str
val slen = List.length xstr
in
String.implode(List.take(xstr, (slen-1)))
end
fun collectLines ls s =
case TextIO.inputLine s of
SOME(l) => collectLines (chomp l::ls) s
| NONE => ls
in
List.rev (collectLines [] strm) before TextIO.closeIn strm
end
</syntaxhighlight>
=={{header|Tcl}}==
Line 3,769 ⟶ 3,821:
=={{header|Wren}}==
<syntaxhighlight lang="
var lines = [] // store lines read
|