Read a file line by line: Difference between revisions

Add Standard ML version
No edit summary
(Add Standard ML version)
 
(11 intermediate revisions by 9 users not shown)
Line 699:
 
</syntaxhighlight>
 
=={{header|Arturo}}==
 
<syntaxhighlight lang="arturo">loop read.lines "myfile.txt" 'line ->
repeat 2drop ;print line</syntaxhighlight>
 
=={{header|Astro}}==
Line 1,483 ⟶ 1,488:
 
=={{header|Elena}}==
ELENA 46.x :
<syntaxhighlight lang="elena">import system'io;
import extensions;
Line 1,490 ⟶ 1,495:
public program()
{
File.assign:("file.txt").forEachLine(printingLn)
}</syntaxhighlight>
 
Line 1,690 ⟶ 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
</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' ( filename-addr filename-len -- )
r/o open-file throw ( 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
dup 2r@ read-line throw ( buffer bytes-read flag )
while
( buffer-addr bytes-read )
over swap type cr
repeat
drop free throw
2r> close-file throw drop ;
 
4096 s" infile.txt" read-lines'</syntaxhighlight>
 
=={{header|Fortran}}==
Line 2,149 ⟶ 2,198:
`input` or `inputs`. For example, to compute the maximum of the above sin values:
 
<syntaxhighlight lang="sh">$ seq 0 5 | jq -Rnn '[inputs | tonumber |sin] | max'
0.9092974268256817</syntaxhighlight>
 
Line 2,850 ⟶ 2,899:
<syntaxhighlight lang="phixmonti">include ..\Utilitys.pmt
 
argument 1 get "r" fopen var f>ps
 
true
while
ftps fgets number? if drop fps> fclose false else print true endif
endwhile</syntaxhighlight>
 
Line 3,244 ⟶ 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,250 ⟶ 3,299:
<syntaxhighlight lang="ruby">var fh = File(__FILE__).open_r
while (fh.readline(\var line)) {
printsay line
}</syntaxhighlight>
 
Line 3,300 ⟶ 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,718 ⟶ 3,789:
End Sub</syntaxhighlight>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">
import os
 
Line 3,750 ⟶ 3,821:
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">import "io" for File
 
var lines = [] // store lines read
23

edits