Read a file line by line: Difference between revisions

Add Standard ML version
(added Arturo)
(Add Standard ML version)
(6 intermediate revisions by 5 users not shown)
Line 700:
</syntaxhighlight>
 
=={{header|arturoArturo}}==
 
<syntaxhighlight lang="arturo">loop read.lines "myfile.txt" 'line ->
Line 1,488:
 
=={{header|Elena}}==
ELENA 46.x :
<syntaxhighlight lang="elena">import system'io;
import extensions;
Line 1,495:
public program()
{
File.assign:("file.txt").forEachLine(printingLn)
}</syntaxhighlight>
 
Line 1,695:
=={{header|Forth}}==
 
<syntaxhighlight lang="forth">4096\ constantThe max-linescratch area provided by PAD is always at least 84 characters in
\ 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 third( readfileid pad max-line throw)
while pad swap third ( fileid cpad max-addrline ufileid ) \ string excludes the newline
read-line throw ( fileid chars-read )
2drop
while pad swap ( fileid pad chars-read ) \ string excludes the newline
repeat 2drop ;</syntaxhighlight>
2droptype cr
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
repeat 2drop ;</syntaxhighlight>
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' ( max filename-addr filename-len -- )
r/wo open-file throw >r( buffer-len wfileid )
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
paddup over r2r@ read-line throw ( buffer bytes-read flag )
while
pad swap ( cbuffer-addr ubytes-read )
cr over swap type cr
repeat r> close-file throw 2drop ;
drop free throw
2r> close-file throw drop ;
 
4096 s" /Users/johnSmith/inputinfile.ftxt" read-lines'</syntaxhighlight>
 
=={{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|
printsay line
}</syntaxhighlight>
 
Line 3,269 ⟶ 3,299:
<syntaxhighlight lang="ruby">var fh = File(__FILE__).open_r
while (fh.readline(\var line)) {
printsay 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="ecmascriptwren">import "io" for File
 
var lines = [] // store lines read
23

edits