Input loop: Difference between revisions
jq |
PascalABC.NET |
||
(125 intermediate revisions by 68 users not shown) | |||
Line 1: | Line 1: | ||
{{task|Text processing}} |
|||
{{task|Text processing}}{{selection|Short Circuit|Console Program Basics}}[[Category:Basic language learning]]Read from a text stream either word-by-word or line-by-line until the stream runs out of data. The stream will have an unknown amount of data on it. |
|||
{{selection|Short Circuit|Console Program Basics}} |
|||
[[Category:Basic language learning]] |
|||
[[Category:Streams]] |
|||
[[Category:Simple]] |
|||
{{omit from|PARI/GP|No access to streams other than input}} |
|||
{{omit from|TI-89 BASIC}} <!-- No streams other than user input, not really applicable. --> |
|||
;Task: |
|||
Read from a text stream either word-by-word or line-by-line until the stream runs out of data. |
|||
The stream will have an unknown amount of data on it. |
|||
<br><br> |
|||
=={{header|AArch64 Assembly}}== |
|||
{{works with|as|Raspberry Pi 3B version Buster 64 bits}} |
|||
<syntaxhighlight lang="aarch64 assembly"> |
|||
/* ARM assembly AARCH64 Raspberry PI 3B */ |
|||
/* program inputLoop64.s */ |
|||
/*******************************************/ |
|||
/* Constantes file */ |
|||
/*******************************************/ |
|||
/* for this file see task include a file in language AArch64 assembly*/ |
|||
.include "../includeConstantesARM64.inc" |
|||
.equ BUFSIZE, 10000 |
|||
.equ LINESIZE, 100 |
|||
/*********************************/ |
|||
/* Initialized data */ |
|||
/*********************************/ |
|||
.data |
|||
szMessErreur: .asciz "Erreur ouverture fichier input.\n" |
|||
szMessErreur1: .asciz "Erreur fermeture fichier.\n" |
|||
szMessErreur2: .asciz "Erreur lecture fichier.\n" |
|||
szCarriageReturn: .asciz "\n" |
|||
szMessEndLine: .asciz "<<<<<< End line.\n" |
|||
szMessCodeErr: .asciz "Error code décimal : @ \n" |
|||
szNameFileInput: .asciz "input.txt" |
|||
/*******************************************/ |
|||
/* DONNEES NON INITIALISEES */ |
|||
/*******************************************/ |
|||
.bss |
|||
sZoneConv: .skip 24 |
|||
sBuffer: .skip BUFSIZE |
|||
sBufferWord: .skip LINESIZE |
|||
/**********************************************/ |
|||
/* -- Code section */ |
|||
/**********************************************/ |
|||
.text |
|||
.global main |
|||
main: |
|||
/* open file */ |
|||
mov x0,AT_FDCWD |
|||
ldr x1,qAdrszNameFileInput // file name |
|||
mov x2,O_RDONLY // flags |
|||
mov x3,0 // mode |
|||
mov x8,OPEN // call system OPEN |
|||
svc 0 |
|||
cmp x0,0 // open error ? |
|||
ble erreur |
|||
/* read file */ |
|||
mov x9,x0 // save File Descriptor |
|||
ldr x1,qAdrsBuffer // buffer address |
|||
mov x2,BUFSIZE // buffer size |
|||
mov x8,READ // call system READ |
|||
svc 0 |
|||
cmp x0,0 // read error ? |
|||
ble erreur2 |
|||
mov x2,x0 // length read characters |
|||
/* buffer analyze */ |
|||
ldr x3,qAdrsBuffer // buffer address |
|||
ldr x5,qAdrsBufferWord // buffer address |
|||
mov x7,0 // word byte counter |
|||
mov x4,0 // byte counter |
|||
mov x10,1 |
|||
1: |
|||
ldrb w6,[x3,x4] // load byte buffer |
|||
cmp x6,' ' // space ? |
|||
csel x8,xzr,x10,eq // yes 0-> x8 |
|||
beq 2f |
|||
cmp x6,0xA // end line ? |
|||
csel x8,x10,xzr,eq // yes 1 -> x8 |
|||
beq 2f |
|||
cmp x6,0xD // end line ? |
|||
csel x8,x10,xzr,eq |
|||
beq 3f |
|||
strb w6,[x5,x7] // store byte |
|||
add x7,x7,1 // increment word byte counter |
|||
b 4f |
|||
2: // word end |
|||
cmp x7,0 |
|||
beq 3f |
|||
mov x6,0 // store 0 final |
|||
strb w6,[x5,x7] |
|||
mov x0,x5 // display word |
|||
bl affichageMess |
|||
ldr x0,qAdrszCarriageReturn |
|||
bl affichageMess |
|||
mov x7,0 // raz word byte counter |
|||
3: |
|||
cmp x8,1 // end line ? |
|||
bne 4f |
|||
ldr x0,qAdrszMessEndLine |
|||
bl affichageMess |
|||
4: |
|||
add x4,x4,1 // increment read buffer counter |
|||
cmp x4,x2 // end bytes ? |
|||
blt 1b // no -> loop |
|||
4: |
|||
/* close imput file */ |
|||
mov x0,x9 // Fd |
|||
mov x8,CLOSE // call system CLOSE |
|||
svc 0 |
|||
cmp x0,0 // close error ? |
|||
blt erreur1 |
|||
mov x0,0 // return code OK |
|||
b 100f |
|||
erreur: |
|||
ldr x1,qAdrszMessErreur |
|||
bl displayError |
|||
mov x0,1 // error return code |
|||
b 100f |
|||
erreur1: |
|||
ldr x1,qAdrszMessErreur1 |
|||
bl displayError |
|||
mov x0,1 // error return code |
|||
b 100f |
|||
erreur2: |
|||
ldr x1,qAdrszMessErreur2 |
|||
bl displayError |
|||
mov x0,1 // error return code |
|||
b 100f |
|||
100: // end program |
|||
mov x8,EXIT |
|||
svc 0 |
|||
qAdrszNameFileInput: .quad szNameFileInput |
|||
qAdrszMessErreur: .quad szMessErreur |
|||
qAdrszMessErreur1: .quad szMessErreur1 |
|||
qAdrszMessErreur2: .quad szMessErreur2 |
|||
qAdrszCarriageReturn: .quad szCarriageReturn |
|||
qAdrszMessEndLine: .quad szMessEndLine |
|||
qAdrsBuffer: .quad sBuffer |
|||
qAdrsBufferWord: .quad sBufferWord |
|||
/******************************************************************/ |
|||
/* display error message */ |
|||
/******************************************************************/ |
|||
/* x0 contains error code */ |
|||
/* x1 contains address error message */ |
|||
displayError: |
|||
stp x2,lr,[sp,-16]! // save registers |
|||
mov x2,x0 // save error code |
|||
mov x0,x1 // display message error |
|||
bl affichageMess |
|||
mov x0,x2 |
|||
ldr x1,qAdrsZoneConv // conversion error code |
|||
bl conversion10S // decimal conversion |
|||
ldr x0,qAdrszMessCodeErr |
|||
ldr x1,qAdrsZoneConv |
|||
bl strInsertAtCharInc // insert result at @ character |
|||
bl affichageMess // display message final |
|||
ldp x2,lr,[sp],16 // restaur 2 registers |
|||
ret // return to address lr x30 |
|||
qAdrsZoneConv: .quad sZoneConv |
|||
qAdrszMessCodeErr: .quad szMessCodeErr |
|||
/********************************************************/ |
|||
/* File Include fonctions */ |
|||
/********************************************************/ |
|||
/* for this file see task include a file in language AArch64 assembly */ |
|||
.include "../includeARM64.inc" |
|||
</syntaxhighlight> |
|||
=={{header|Action!}}== |
|||
<syntaxhighlight lang="action!">PROC ReadStream(BYTE stream) |
|||
CHAR ARRAY line(255) |
|||
WHILE Eof(stream)=0 |
|||
DO |
|||
InputSD(stream,line) |
|||
PrintE(line) |
|||
OD |
|||
RETURN |
|||
PROC Main() |
|||
BYTE streamId=[1] |
|||
Close(streamId) |
|||
Open(streamId,"H6:INPUT_PU.ACT",4) |
|||
PrintE("Reading from stream...") PutE() |
|||
ReadStream(streamId) |
|||
Close(streamId) |
|||
RETURN</syntaxhighlight> |
|||
{{out}} |
|||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Input_loop.png Screenshot from Atari 8-bit computer] |
|||
<pre> |
|||
Reading from stream... |
|||
PROC ReadStream(BYTE stream) |
|||
CHAR ARRAY line(255) |
|||
WHILE Eof(stream)=0 |
|||
DO |
|||
InputSD(stream,line) |
|||
PrintE(line) |
|||
OD |
|||
RETURN |
|||
PROC Main() |
|||
BYTE streamId=[1] |
|||
Close(streamId) |
|||
Open(streamId,"H6:INPUT_PU.ACT",4) |
|||
PrintE("Reading from stream...") PutE() |
|||
ReadStream(streamId) |
|||
Close(streamId) |
|||
RETURN |
|||
</pre> |
|||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
This example reads in a text stream from standard input line by line |
This example reads in a text stream from standard input line by line |
||
and writes the output to standard output. |
|||
<lang ada>with Ada.Text_Io; use Ada.Text_Io; |
|||
<syntaxhighlight lang="ada">with Ada.Text_Io; use Ada.Text_Io; |
|||
procedure Read_Stream is |
procedure Read_Stream is |
||
Line 20: | Line 244: | ||
end if; |
end if; |
||
end loop; |
end loop; |
||
end Read_Stream;</ |
end Read_Stream;</syntaxhighlight> |
||
=={{header|Aime}}== |
=={{header|Aime}}== |
||
< |
<syntaxhighlight lang="aime">void |
||
read_stream(file f) |
read_stream(file f) |
||
{ |
{ |
||
Line 31: | Line 255: | ||
# the read line available as -s- |
# the read line available as -s- |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
For file consisting of just one page - a typical linux/unix file: |
For file consisting of just one page - a typical linux/unix file: |
||
< |
<syntaxhighlight lang="algol68">main:( |
||
PROC raise logical file end = (REF FILE f) BOOL: ( except logical file end ); |
PROC raise logical file end = (REF FILE f) BOOL: ( except logical file end ); |
||
on logical file end(stand in, raise logical file end); |
on logical file end(stand in, raise logical file end); |
||
Line 45: | Line 269: | ||
except logical file end: |
except logical file end: |
||
SKIP |
SKIP |
||
)</ |
)</syntaxhighlight> |
||
For multi page files, each page is seekable with ''<tt>PROC set = (REF FILE file, INT page, line, char)VOID: ~</tt>''. This allows rudimentary random access where each new page is effectively a new record. |
For multi page files, each page is seekable with ''<tt>PROC set = (REF FILE file, INT page, line, char)VOID: ~</tt>''. This allows rudimentary random access where each new page is effectively a new record. |
||
< |
<syntaxhighlight lang="algol68">main:( |
||
PROC raise logical file end = (REF FILE f) BOOL: ( except logical file end ); |
PROC raise logical file end = (REF FILE f) BOOL: ( except logical file end ); |
||
on logical file end(stand in, raise logical file end); |
on logical file end(stand in, raise logical file end); |
||
Line 64: | Line 288: | ||
except logical file end: |
except logical file end: |
||
SKIP |
SKIP |
||
)</ |
)</syntaxhighlight> |
||
The boolean functions ''physical file ended(f)'', ''logical file ended(f)'', ''page ended(f)'' and ''line ended(f)'' are also available to indicate the end of a file, page and line. |
The boolean functions ''physical file ended(f)'', ''logical file ended(f)'', ''page ended(f)'' and ''line ended(f)'' are also available to indicate the end of a file, page and line. |
||
=={{header|ALGOL W}}== |
|||
<syntaxhighlight lang="algolw">begin |
|||
string(80) line; |
|||
% allow the program to continue after reaching end-of-file % |
|||
% without this, end-of-file would cause a run-time error % |
|||
ENDFILE := EXCEPTION( false, 1, 0, false, "EOF" ); |
|||
% read lines until end of file % |
|||
read( line ); |
|||
while not XCPNOTED(ENDFILE) do begin |
|||
write( line ); |
|||
read( line ) |
|||
end |
|||
end.</syntaxhighlight> |
|||
=={{header|AmigaE}}== |
=={{header|AmigaE}}== |
||
< |
<syntaxhighlight lang="amigae">CONST BUFLEN=1024, EOF=-1 |
||
PROC consume_input(fh) |
PROC consume_input(fh) |
||
Line 92: | Line 331: | ||
Close(fh) |
Close(fh) |
||
ENDIF |
ENDIF |
||
ENDPROC</ |
ENDPROC</syntaxhighlight> |
||
=={{header|APL}}== |
|||
{{works with|GNU APL}} |
|||
<syntaxhighlight lang="apl"> |
|||
h ← ⊃ (⎕fio['read_text'] 'corpus/sample1.txt') |
|||
⍴h |
|||
7 49 |
|||
]boxing 8 |
|||
h |
|||
┌→────────────────────────────────────────────────┐ |
|||
↓This is some sample text. │ |
|||
│The text itself has multiple lines, and │ |
|||
│the text has some words that occur multiple times│ |
|||
│in the text. │ |
|||
│ │ |
|||
│This is the end of the text. │ |
|||
│ │ |
|||
└─────────────────────────────────────────────────┘ |
|||
</syntaxhighlight> |
|||
=={{header|ARM Assembly}}== |
|||
{{works with|as|Raspberry Pi}} |
|||
<syntaxhighlight lang="arm assembly"> |
|||
/* ARM assembly Raspberry PI */ |
|||
/* program inputLoop.s */ |
|||
/* REMARK 1 : this program use routines in a include file |
|||
see task Include a file language arm assembly |
|||
for the routine affichageMess conversion10 |
|||
see at end of this program the instruction include */ |
|||
/*********************************************/ |
|||
/*constantes */ |
|||
/********************************************/ |
|||
.equ STDOUT, 1 @ Linux output console |
|||
.equ EXIT, 1 @ Linux syscall |
|||
.equ READ, 3 |
|||
.equ WRITE, 4 |
|||
.equ OPEN, 5 |
|||
.equ CLOSE, 6 |
|||
.equ CREATE, 8 |
|||
/* file */ |
|||
.equ O_RDONLY, 0x0 @ open for reading only |
|||
.equ BUFSIZE, 10000 |
|||
.equ LINESIZE, 100 |
|||
/*********************************/ |
|||
/* Initialized data */ |
|||
/*********************************/ |
|||
.data |
|||
szMessErreur: .asciz "Erreur ouverture fichier input.\n" |
|||
szMessErreur1: .asciz "Erreur fermeture fichier.\n" |
|||
szMessErreur2: .asciz "Erreur lecture fichier.\n" |
|||
szCarriageReturn: .asciz "\n" |
|||
szMessEndLine: .asciz "<<<<<< End line.\n" |
|||
szNameFileInput: .asciz "input.txt" |
|||
/*******************************************/ |
|||
/* DONNEES NON INITIALISEES */ |
|||
/*******************************************/ |
|||
.bss |
|||
sBuffer: .skip BUFSIZE |
|||
sBufferWord: .skip LINESIZE |
|||
/**********************************************/ |
|||
/* -- Code section */ |
|||
/**********************************************/ |
|||
.text |
|||
.global main |
|||
main: |
|||
/* open file */ |
|||
ldr r0,iAdrszNameFileInput @ file name |
|||
mov r1,#O_RDONLY @ flags |
|||
mov r2,#0 @ mode |
|||
mov r7,#OPEN @ call system OPEN |
|||
svc #0 |
|||
cmp r0,#0 @ open error ? |
|||
ble erreur |
|||
/* read file */ |
|||
mov r9,r0 @ save File Descriptor |
|||
ldr r1,iAdrsBuffer @ buffer address |
|||
mov r2,#BUFSIZE @ buffer size |
|||
mov r7, #READ @ call system READ |
|||
svc 0 |
|||
cmp r0,#0 @ read error ? |
|||
ble erreur2 |
|||
mov r2,r0 @ length read characters |
|||
/* buffer analyze */ |
|||
ldr r3,iAdrsBuffer @ buffer address |
|||
ldr r5,iAdrsBufferWord @ buffer address |
|||
mov r7,#0 @ word byte counter |
|||
mov r4,#0 @ byte counter |
|||
1: |
|||
ldrb r6,[r3,r4] @ load byte buffer |
|||
cmp r6,#' ' @ space ? |
|||
moveq r8,#0 @ yes |
|||
beq 2f |
|||
cmp r6,#0xA @ end line ? |
|||
moveq r8,#1 |
|||
beq 2f |
|||
cmp r6,#0xD @ end line ? |
|||
beq 3f |
|||
strb r6,[r5,r7] @ store byte |
|||
add r7,#1 @ increment word byte counter |
|||
b 4f |
|||
2: @ word end |
|||
cmp r7,#0 |
|||
beq 3f |
|||
mov r6,#0 @ store 0 final |
|||
strb r6,[r5,r7] |
|||
mov r0,r5 @ display word |
|||
bl affichageMess |
|||
ldr r0,iAdrszCarriageReturn |
|||
bl affichageMess |
|||
mov r7,#0 @ raz word byte counter |
|||
3: |
|||
cmp r8,#1 @ end line ? |
|||
bne 4f |
|||
ldr r0,iAdrszMessEndLine |
|||
bl affichageMess |
|||
4: |
|||
add r4,#1 @ increment read buffer counter |
|||
cmp r4,r2 @ end bytes ? |
|||
blt 1b @ no -> loop |
|||
4: |
|||
/* close imput file */ |
|||
mov r0,r9 @ Fd |
|||
mov r7, #CLOSE @ call system CLOSE |
|||
svc 0 |
|||
cmp r0,#0 @ close error ? |
|||
blt erreur1 |
|||
mov r0,#0 @ return code OK |
|||
b 100f |
|||
erreur: |
|||
ldr r1,iAdrszMessErreur |
|||
bl displayError |
|||
mov r0,#1 @ error return code |
|||
b 100f |
|||
erreur1: |
|||
ldr r1,iAdrszMessErreur1 |
|||
bl displayError |
|||
mov r0,#1 @ error return code |
|||
b 100f |
|||
erreur2: |
|||
ldr r1,iAdrszMessErreur2 |
|||
bl displayError |
|||
mov r0,#1 @ error return code |
|||
b 100f |
|||
100: @ end program |
|||
mov r7, #EXIT |
|||
svc 0 |
|||
iAdrszNameFileInput: .int szNameFileInput |
|||
iAdrszMessErreur: .int szMessErreur |
|||
iAdrszMessErreur1: .int szMessErreur1 |
|||
iAdrszMessErreur2: .int szMessErreur2 |
|||
iAdrszCarriageReturn: .int szCarriageReturn |
|||
iAdrszMessEndLine: .int szMessEndLine |
|||
iAdrsBuffer: .int sBuffer |
|||
iAdrsBufferWord: .int sBufferWord |
|||
/***************************************************/ |
|||
/* ROUTINES INCLUDE */ |
|||
/***************************************************/ |
|||
.include "../affichage.inc" |
|||
</syntaxhighlight> |
|||
{{Output}} |
|||
<pre> |
|||
word1 |
|||
word2 |
|||
word3 |
|||
<<<<<< End line. |
|||
Line2w1 |
|||
line2w2 |
|||
<<<<<< End line. |
|||
Oneword |
|||
<<<<<< End line. |
|||
<<<<<< End line. |
|||
AfterEmptyLine |
|||
<<<<<< End line. |
|||
</pre> |
|||
=={{header|Arturo}}== |
|||
<syntaxhighlight lang="arturo">while [true][ |
|||
i: input "> " |
|||
print i |
|||
]</syntaxhighlight> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
This example reads the text of a source file line by line |
This example reads the text of a source file line by line |
||
and writes the output to a destination file. |
|||
<lang AutoHotkey>Loop, Read, Input.txt, Output.txt |
|||
<syntaxhighlight lang="autohotkey">Loop, Read, Input.txt, Output.txt |
|||
{ |
{ |
||
FileAppend, %A_LoopReadLine%`n |
FileAppend, %A_LoopReadLine%`n |
||
}</ |
}</syntaxhighlight> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
This just reads lines from stdin and prints them until EOF is read. |
This just reads lines from stdin and prints them until EOF is read. |
||
<lang |
<syntaxhighlight lang="awk">{ print $0 }</syntaxhighlight> |
||
or, more idiomatic: |
or, more idiomatic: |
||
<lang |
<syntaxhighlight lang="awk">1</syntaxhighlight> |
||
=={{header|Batch File}}== |
|||
<lang dos> |
|||
for /f %%i in (file.txt) do if %%i@ neq @ echo %%i |
|||
</lang> |
|||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
==={{header|BASIC256}}=== |
|||
<syntaxhighlight lang="basic256">f = freefile |
|||
open f, "test.txt" |
|||
while not eof(f) |
|||
linea$ = readline(f) |
|||
print linea$ # echo to the console |
|||
end while |
|||
close f |
|||
end</syntaxhighlight> |
|||
==={{header|OxygenBasic}}=== |
|||
<syntaxhighlight lang="text"> |
|||
uses Console |
|||
uses ParseUtil |
|||
string s=getfile "t.txt" |
|||
int le=len s |
|||
int i=1 |
|||
while i<le |
|||
print GetNextLine(s,i) cr |
|||
wend |
|||
pause |
|||
</syntaxhighlight> |
|||
==={{header|QBasic}}=== |
|||
{{works with|QBasic}} |
|||
{{works with|QuickBasic}} |
|||
<syntaxhighlight lang="qbasic">OPEN "test.txt" FOR INPUT AS #1 |
|||
WHILE NOT EOF(1) |
|||
INPUT #1, linea$ |
|||
PRINT linea$ ' echo to the console |
|||
WEND |
|||
CLOSE #1 |
|||
END</syntaxhighlight> |
|||
==={{header|True BASIC}}=== |
|||
<syntaxhighlight lang="qbasic">OPEN #1: NAME "test.txt", ACCESS INPUT |
|||
DO |
|||
LINE INPUT #1: linea$ |
|||
PRINT linea$ ! echo to the console |
|||
LOOP UNTIL END #1 |
|||
CLOSE #1 |
|||
END</syntaxhighlight> |
|||
==={{header|Yabasic}}=== |
|||
<syntaxhighlight lang="yabasic">filename$ = "test.txt" |
|||
f = open(filename$) |
|||
if not f error "Could not open '" + filename$ + "' for reading" |
|||
while(not eof(f)) |
|||
line input #f linea$ |
|||
print linea$ // echo to the console |
|||
wend |
|||
close f |
|||
end</syntaxhighlight> |
|||
==={{header|Applesoft BASIC}}=== |
==={{header|Applesoft BASIC}}=== |
||
< |
<syntaxhighlight lang="applesoftbasic">100 INPUT "FILENAME:";F$ |
||
110 D$ = CHR$(4) |
110 D$ = CHR$(4) |
||
120 PRINT D$"VERIFY"F$ |
120 PRINT D$"VERIFY"F$ |
||
Line 131: | Line 620: | ||
190 POKE 216,0 |
190 POKE 216,0 |
||
200 IF PEEK(222) <> 5 THEN RESUME |
200 IF PEEK(222) <> 5 THEN RESUME |
||
210 PRINT D$"CLOSE"F$</ |
210 PRINT D$"CLOSE"F$</syntaxhighlight> |
||
==={{header|BaCon}}=== |
|||
<syntaxhighlight lang="freebasic"> |
|||
'--- some generic header file to give it a real test |
|||
PRINT "Enter any file name you want to read ex: /usr/include/X11/X.h" |
|||
INPUT filename$ |
|||
text$ = LOAD$(filename$) |
|||
SPLIT text$ BY NL$ TO TOK$ SIZE dim |
|||
i = 0 |
|||
'---dynamic index the end of an array is always null terminated |
|||
WHILE (TOK$[i] ISNOT NULL) |
|||
PRINT TOK$[i] |
|||
INCR i |
|||
WEND |
|||
</syntaxhighlight> |
|||
==={{header|BBC BASIC}}=== |
==={{header|BBC BASIC}}=== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
This specifically relates to console input (stdin). |
This specifically relates to console input (stdin). |
||
< |
<syntaxhighlight lang="bbcbasic"> STD_INPUT_HANDLE = -10 |
||
STD_OUTPUT_HANDLE = -11 |
STD_OUTPUT_HANDLE = -11 |
||
SYS "GetStdHandle", STD_INPUT_HANDLE TO @hfile%(1) |
SYS "GetStdHandle", STD_INPUT_HANDLE TO @hfile%(1) |
||
Line 146: | Line 653: | ||
INPUT A$ |
INPUT A$ |
||
PRINT A$ |
PRINT A$ |
||
UNTIL FALSE</ |
UNTIL FALSE</syntaxhighlight> |
||
==={{header|Commodore BASIC}}=== |
|||
This example should generally work across all Commodore models. It is important to note that when using the reserved variable STATUS (or ST for short) to detect the end of file marker (bit 6), the KERNAL updates this variable based on ''any'' previous I/O operation, including the OPEN command, but makes no indication as to which I/O access the status applies to. Therefore, if using the disk drive, it is best to open the command file number and channel (15) ''before'' opening the actual file on disk, and leave the command channel open while performing the operations to open the file and access it as shown in the example. |
|||
<syntaxhighlight lang="commodorebasicv2"> |
|||
10 rem input loop - rosetta code |
|||
11 rem open command channel, clear screen, switch to lower case |
|||
12 open 15,8,15 |
|||
15 print chr$(147);chr$(14):f$="" |
|||
20 input "Enter filename";f$ |
|||
25 if f$="" then end |
|||
30 open 5,8,5,f$+",s,r" |
|||
40 gosub 1000 |
|||
50 if er=62 then print "That file is not found... Try again.":close 5:goto 20 |
|||
60 if er<>0 then print "There was an unexpected error.":close 5:gosub 1100 |
|||
70 get#5,a$ |
|||
80 if st and 64 then close 5:close 15:end |
|||
90 print a$;:goto 70 |
|||
1000 rem check command channel for error |
|||
1005 rem error number, error msg$, track number, sector number |
|||
1010 input#15,er,er$,tk,sc |
|||
1020 return |
|||
1100 rem print error |
|||
1110 print:print er;"- ";er$;" track:";tk;"sector:";sc |
|||
1120 return |
|||
</syntaxhighlight> |
|||
==={{header|IS-BASIC}}=== |
|||
<syntaxhighlight lang="is-basic">100 PROGRAM "Type.bas" |
|||
110 TEXT 80 |
|||
120 INPUT PROMPT "File name: ":F$ |
|||
130 WHEN EXCEPTION USE IOERROR |
|||
140 OPEN #1:F$ ACCESS INPUT |
|||
150 DO |
|||
160 LINE INPUT #1,IF MISSING EXIT DO:F$ |
|||
170 PRINT F$ |
|||
180 LOOP |
|||
190 CLOSE #1 |
|||
200 END WHEN |
|||
210 HANDLER IOERROR |
|||
220 PRINT EXSTRING$(EXTYPE) |
|||
230 END |
|||
240 END HANDLER</syntaxhighlight> |
|||
Alternate solution: |
|||
<syntaxhighlight lang="is-basic">100 PROGRAM "Type.bas" |
|||
110 INPUT PROMPT "File name: ":F$ |
|||
120 WHEN EXCEPTION USE IOERROR |
|||
130 OPEN #1:F$ |
|||
140 COPY FROM #1 TO #0 |
|||
150 CLOSE #1 |
|||
160 END WHEN |
|||
170 HANDLER IOERROR |
|||
180 PRINT EXSTRING$(EXTYPE) |
|||
190 CLOSE #1 |
|||
200 END HANDLER</syntaxhighlight> |
|||
==={{header|uBasic/4tH}}=== |
|||
<syntaxhighlight lang="text">If Set (a, Open ("myfile.bas", "r")) < 0 Then Print "Cannot open \qmyfile.bas\q" : End |
|||
Do While Read (a) |
|||
Print Show(Tok(0)) |
|||
Loop |
|||
Close a |
|||
</syntaxhighlight> |
|||
This version tokenizes the line read and prints all individual words. |
|||
<syntaxhighlight lang="text">If Set (a, Open ("myfile.bas", "r")) < 0 Then Print "Cannot open \qmyfile.bas\q" : End |
|||
Do While Read (a) ' get next line |
|||
Do |
|||
p = Here() ' save cursor position |
|||
s = Tok(Ord(" ")) ' tokenize next word |
|||
Until p = Here() ' quit if cursor didn't progress |
|||
If Len(s) Then Print Show(s) ' print only full words |
|||
Loop ' next word |
|||
Loop ' next line |
|||
Close a |
|||
</syntaxhighlight> |
|||
==={{header|SmallBASIC}}=== |
|||
Open a text file and print content line by line: |
|||
<syntaxhighlight lang="qbasic"> |
|||
open "input.txt" for input as #1 |
|||
while(!eof(1)) |
|||
input #1, c |
|||
print c |
|||
wend |
|||
close #1 |
|||
</syntaxhighlight> |
|||
=={{header|Batch File}}== |
|||
<syntaxhighlight lang="dos"> |
|||
for /f %%i in (file.txt) do if %%i@ neq @ echo %%i |
|||
</syntaxhighlight> |
|||
=={{header|Bracmat}}== |
=={{header|Bracmat}}== |
||
This example first creates a test file with three lines. It then opens the file in read mode, sets the string of break characters and then reads the file token by token, where tokens are delimeted by break characters. Finally, the file position is set to an invalid value, which closes the file. |
This example first creates a test file with three lines. It then opens the file in read mode, sets the string of break characters and then reads the file token by token, where tokens are delimeted by break characters. Finally, the file position is set to an invalid value, which closes the file. |
||
< |
<syntaxhighlight lang="bracmat">( put$("This is |
||
a three line |
a three line |
||
text","test.txt",NEW) |
text","test.txt",NEW) |
||
Line 176: | Line 783: | ||
) |
) |
||
& (fil$(,SET,-1)|out$"file closed") |
& (fil$(,SET,-1)|out$"file closed") |
||
);</ |
);</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre>breakchar:SP, word 1:This |
<pre>breakchar:SP, word 1:This |
||
breakchar:\n, word 2:is |
breakchar:\n, word 2:is |
||
Line 187: | Line 794: | ||
=={{header|C}}== |
=={{header|C}}== |
||
Reads arbitrarily long line each time and return a null-terminated string. |
Reads arbitrarily long line each time and return a null-terminated string. |
||
Caller is responsible for freeing the string. |
|||
<lang c>#include <stdlib.h> |
|||
<syntaxhighlight lang="c">#include <stdlib.h> |
|||
#include <stdio.h> |
#include <stdio.h> |
||
Line 219: | Line 827: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|C sharp|C#}}== |
|||
<syntaxhighlight lang="csharp">using System; |
|||
using System.IO; |
|||
class Program |
|||
{ |
|||
static void Main(string[] args) |
|||
{ |
|||
// For stdin, you could use |
|||
// new StreamReader(Console.OpenStandardInput(), Console.InputEncoding) |
|||
using (var b = new StreamReader("file.txt")) |
|||
{ |
|||
string line; |
|||
while ((line = b.ReadLine()) != null) |
|||
Console.WriteLine(line); |
|||
} |
|||
} |
|||
}</syntaxhighlight> |
|||
=={{header|C++}}== |
=={{header|C++}}== |
||
The following functions send the words resp. lines |
The following functions send the words resp. lines |
||
to a generic output iterator. |
|||
<lang cpp> |
|||
<syntaxhighlight lang="cpp"> |
|||
#include <istream> |
#include <istream> |
||
#include <string> |
#include <string> |
||
Line 268: | Line 898: | ||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
An alternate way to read words or lines is to use istream iterators: |
An alternate way to read words or lines is to use istream iterators: |
||
< |
<syntaxhighlight lang="cpp"> |
||
template<class OutIt> |
template<class OutIt> |
||
void read_words(std::istream& is, OutIt dest) |
void read_words(std::istream& is, OutIt dest) |
||
Line 299: | Line 929: | ||
dest); |
dest); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|C sharp|C#}}== |
|||
<lang csharp>using System; |
|||
using System.IO; |
|||
class Program |
|||
{ |
|||
static void Main(string[] args) |
|||
{ |
|||
StreamReader b = new StreamReader("file.txt"); //or any other Stream |
|||
string line = b.ReadLine(); |
|||
while (line != null) |
|||
{ |
|||
Console.WriteLine(line); |
|||
line = b.ReadLine(); |
|||
} |
|||
} |
|||
}</lang> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="lisp">(defn basic-input [fname] |
||
(line-seq (java.io.BufferedReader. (java.io.FileReader. fname))))</ |
(line-seq (java.io.BufferedReader. (java.io.FileReader. fname))))</syntaxhighlight> |
||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
{{works with|GNU Cobol|2.0}} |
{{works with|GNU Cobol|2.0}} |
||
{{works with|Visual COBOL}} |
{{works with|Visual COBOL}} |
||
< |
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION. |
||
PROGRAM-ID. input-loop. |
PROGRAM-ID. input-loop. |
||
Line 361: | Line 970: | ||
CLOSE in-stream |
CLOSE in-stream |
||
. |
. |
||
END PROGRAM input-loop.</ |
END PROGRAM input-loop.</syntaxhighlight> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp">(defun basic-input (filename) |
||
(with-open-file (stream (make-pathname :name filename) :direction :input) |
(with-open-file (stream (make-pathname :name filename) :direction :input) |
||
(loop for line = (read-line stream nil nil) |
(loop for line = (read-line stream nil nil) |
||
while line |
while line |
||
do (format t "~a~%" line))))</ |
do (format t "~a~%" line))))</syntaxhighlight> |
||
=={{header|D}}== |
=={{header|D}}== |
||
< |
<syntaxhighlight lang="d">void main() { |
||
import std.stdio; |
import std.stdio; |
||
Line 393: | Line 1,002: | ||
line.writeln; |
line.writeln; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{libheader|Tango}} |
{{libheader|Tango}} |
||
< |
<syntaxhighlight lang="d">import tango.io.Console; |
||
import tango.text.stream.LineIterator; |
import tango.text.stream.LineIterator; |
||
Line 403: | Line 1,012: | ||
// do something with each line |
// do something with each line |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{libheader|Tango}} |
{{libheader|Tango}} |
||
< |
<syntaxhighlight lang="d">import tango.io.Console; |
||
import tango.text.stream.SimpleIterator; |
import tango.text.stream.SimpleIterator; |
||
Line 412: | Line 1,021: | ||
// do something with each word |
// do something with each word |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Note that foreach variables 'line' and 'word' are transient slices. If you need to retain them for later use, you should .dup them. |
Note that foreach variables 'line' and 'word' are transient slices. If you need to retain them for later use, you should .dup them. |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
< |
<syntaxhighlight lang="delphi">program InputLoop; |
||
{$APPTYPE CONSOLE} |
{$APPTYPE CONSOLE} |
||
Line 433: | Line 1,042: | ||
lReader.Free; |
lReader.Free; |
||
end; |
end; |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|Déjà Vu}}== |
=={{header|Déjà Vu}}== |
||
< |
<syntaxhighlight lang="dejavu">while /= :eof dup !read-line!stdin: |
||
!print( "Read a line: " !decode!utf-8 swap ) |
!print( "Read a line: " !decode!utf-8 swap ) |
||
drop |
drop |
||
!print "End of file."</ |
!print "End of file."</syntaxhighlight> |
||
=={{header|EasyLang}}== |
|||
<syntaxhighlight lang="text">repeat |
|||
l$ = input |
|||
until error = 1 |
|||
print l$ |
|||
.</syntaxhighlight> |
|||
=={{header|Eiffel}}== |
=={{header|Eiffel}}== |
||
{{works with|Eiffel Studio|6.6}} |
{{works with|Eiffel Studio|6.6}} |
||
<syntaxhighlight lang="eiffel"> |
|||
<lang Eiffel> |
|||
note |
note |
||
description : "{ |
description : "{ |
||
Line 680: | Line 1,298: | ||
end |
end |
||
</syntaxhighlight> |
|||
=={{header|Elena}}== |
|||
ELENA 6.x: |
|||
Using ReaderEnumerator |
|||
<syntaxhighlight lang="elena">import system'routines; |
|||
import system'io; |
|||
import extensions'routines; |
|||
public program() |
|||
{ |
|||
ReaderEnumerator.new(File.assign("file.txt")).forEach(printingLn) |
|||
}</syntaxhighlight> |
|||
Using loop statement |
|||
<syntaxhighlight lang="elena">import system'io; |
|||
public program() |
|||
{ |
|||
using(var reader := File.assign("file.txt").textreader()) |
|||
{ |
|||
while (reader.Available) |
|||
{ |
|||
console.writeLine(reader.readLine()) |
|||
} |
|||
} |
|||
}</syntaxhighlight> |
|||
=={{header|Elixir}}== |
|||
<syntaxhighlight lang="elixir">defmodule RC do |
|||
def input_loop(stream) do |
|||
case IO.read(stream, :line) do |
|||
:eof -> :ok |
|||
data -> IO.write data |
|||
input_loop(stream) |
|||
end |
|||
end |
|||
end |
|||
path = hd(System.argv) |
|||
File.open!(path, [:read], fn stream -> RC.input_loop(stream) end)</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
< |
<syntaxhighlight lang="erlang">% Implemented by Arjun Sunel |
||
-module(read_files). |
-module(read_files). |
||
-export([main/0]). |
-export([main/0]). |
||
Line 691: | Line 1,349: | ||
Lines = string:tokens(binary_to_list(Read("read_files.erl")), "\n"), |
Lines = string:tokens(binary_to_list(Read("read_files.erl")), "\n"), |
||
lists:foreach(fun (Y) -> io:format("~s~n", [Y]) end, lists:zipwith(fun(X,_)->X end, Lines, lists:seq(1, length(Lines)))). |
lists:foreach(fun (Y) -> io:format("~s~n", [Y]) end, lists:zipwith(fun(X,_)->X end, Lines, lists:seq(1, length(Lines)))). |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|ERRE}}== |
|||
input from stdio |
|||
LOOP |
|||
INPUT(LINE,A$) |
|||
PRINT(A$) |
|||
EXIT IF <condition> ! condition to be implemented to |
|||
! to avoid and endless loop |
|||
END LOOP |
|||
reading a text file line by line |
|||
OPEN("I",#1,FILENAME$) |
|||
WHILE NOT EOF(1) |
|||
INPUT(LINE,#1,A$) |
|||
PRINT(A$) |
|||
END WHILE |
|||
CLOSE(1) |
|||
Note: with GET(#1) you can read character by character. |
|||
=={{header|Euphoria}}== |
=={{header|Euphoria}}== |
||
Process text stream line-by-line: |
Process text stream line-by-line: |
||
< |
<syntaxhighlight lang="euphoria">procedure process_line_by_line(integer fn) |
||
object line |
object line |
||
while 1 do |
while 1 do |
||
Line 704: | Line 1,384: | ||
-- process the line |
-- process the line |
||
end while |
end while |
||
end procedure</ |
end procedure</syntaxhighlight> |
||
=={{header|F Sharp|F#}}== |
=={{header|F Sharp|F#}}== |
||
Using a sequence expression: |
Using a sequence expression: |
||
< |
<syntaxhighlight lang="fsharp"> |
||
let lines_of_file file = |
let lines_of_file file = |
||
seq { use stream = System.IO.File.OpenRead file |
seq { use stream = System.IO.File.OpenRead file |
||
Line 714: | Line 1,394: | ||
while not reader.EndOfStream do |
while not reader.EndOfStream do |
||
yield reader.ReadLine() } |
yield reader.ReadLine() } |
||
</syntaxhighlight> |
|||
</lang> |
|||
The file is reopened every time the sequence is traversed and lines are read on-demand so this can handle arbitrarily-large files. |
The file is reopened every time the sequence is traversed and lines are read on-demand so this can handle arbitrarily-large files. |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
< |
<syntaxhighlight lang="factor">"file.txt" utf8 [ [ process-line ] each-line ] with-file-reader</syntaxhighlight> |
||
=={{header|Fantom}}== |
=={{header|Fantom}}== |
||
Line 724: | Line 1,404: | ||
An input stream can be from a string or from a file. The method <code>eachLine</code> will divide the stream by linebreaks. The method <code>readStrToken</code> takes two arguments: a maximum size to read, and a function to decide when to stop reading - by default, it stops when it finds a white space. |
An input stream can be from a string or from a file. The method <code>eachLine</code> will divide the stream by linebreaks. The method <code>readStrToken</code> takes two arguments: a maximum size to read, and a function to decide when to stop reading - by default, it stops when it finds a white space. |
||
< |
<syntaxhighlight lang="fantom"> |
||
class Main |
class Main |
||
{ |
{ |
||
Line 751: | Line 1,431: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
{{works with|GNU Forth}} |
{{works with|GNU Forth}} |
||
< |
<syntaxhighlight lang="forth">4096 constant max-line |
||
: read-lines |
: read-lines |
||
begin stdin pad max-line read-line throw |
begin stdin pad max-line read-line throw |
||
while pad swap \ addr len is the line of data, excluding newline |
while pad swap \ addr len is the line of data, excluding newline |
||
2drop |
2drop |
||
repeat ;</ |
repeat ;</syntaxhighlight> |
||
=={{header|Fortran}}== |
=={{header|Fortran}}== |
||
Line 766: | Line 1,446: | ||
The code read line-by-line, but the maximum length of the line is limited (by a parameter) |
The code read line-by-line, but the maximum length of the line is limited (by a parameter) |
||
< |
<syntaxhighlight lang="fortran">program BasicInputLoop |
||
implicit none |
implicit none |
||
Line 785: | Line 1,465: | ||
end if |
end if |
||
end program BasicInputLoop</ |
end program BasicInputLoop</syntaxhighlight> |
||
=={{header|FreeBASIC}}== |
|||
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
|||
Dim line_ As String ' line is a keyword |
|||
Open "input.txt" For Input As #1 |
|||
While Not Eof(1) |
|||
Input #1, line_ |
|||
Print line_ ' echo to the console |
|||
Wend |
|||
Close #1 |
|||
Print |
|||
Print "Press any key to quit" |
|||
Sleep</syntaxhighlight> |
|||
=={{header|Frink}}== |
|||
<syntaxhighlight lang="frink">while (line = readStdin[]) != undef |
|||
println[line] |
|||
</syntaxhighlight> |
|||
=={{header|FutureBasic}}== |
|||
Note: This code goes beyond simply specifying the file to open. It includes a dialog window that allows the user to select a text file to read. The entire contents of the file are read in at once, rather than line by line. |
|||
<syntaxhighlight lang="futurebasic">include "NSLog.incl" |
|||
local fn ReadTextFile |
|||
CFURLRef url |
|||
CFStringRef string |
|||
url = openpanel 1, @"Select text file..." |
|||
if ( url ) |
|||
string = fn StringWithContentsOfURL( url, NSUTF8StringEncoding, NULL ) |
|||
if ( string ) |
|||
NSLog(@"%@",string) |
|||
end if |
|||
else |
|||
// user cancelled |
|||
end if |
|||
end fn |
|||
fn ReadTextFile |
|||
HandleEvents</syntaxhighlight> |
|||
=={{header|GDScript}}== |
|||
{{works with|Godot|4.0.1}} |
|||
<syntaxhighlight lang="gdscript"> |
|||
extends MainLoop |
|||
func _process(_delta: float) -> bool: |
|||
while true: |
|||
# Read a line from stdin |
|||
var input: String = OS.read_string_from_stdin() |
|||
# Empty lines are "\n" whereas end of input will be completely empty. |
|||
if len(input) == 0: |
|||
break |
|||
printraw(input) |
|||
return true # Exit |
|||
</syntaxhighlight> |
|||
=={{header|gnuplot}}== |
=={{header|gnuplot}}== |
||
The following gnuplot script echoes standard input |
The following gnuplot script echoes standard input |
||
to standard output line-by-line until the end of the stream. |
|||
<lang gnuplot>!cat</lang> |
|||
<syntaxhighlight lang="gnuplot">!cat</syntaxhighlight> |
|||
It makes use of the ability of gnuplot to spawn shell commands. In that sense it might be considered cheating. Nevertheless, this is a valid gnuplot script that does meet the requirements of the task description. |
|||
It makes use of the ability of gnuplot to spawn shell commands. |
|||
In that sense it might be considered cheating. |
|||
Nevertheless, this is a valid gnuplot script |
|||
that does meet the requirements of the task description. |
|||
It seems impossible to complete this task with just standard gnuplot commands. |
It seems impossible to complete this task with just standard gnuplot commands. |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
The following reads a line at a time |
The following reads a line at a time from stdin. |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
"bufio" |
|||
"io" |
|||
"log" |
|||
"os" |
|||
) |
) |
||
func main() { |
func main() { |
||
in := bufio.NewReader(os.Stdin) |
|||
for { |
|||
s, err := in.ReadString('\n') |
|||
if err != nil { |
|||
// io.EOF is expected, anything else |
|||
break |
|||
// should be handled/reported |
|||
} |
|||
if err != io.EOF { |
|||
_ = s |
|||
log.Fatal(err) |
|||
} |
|||
} |
|||
}</lang> |
|||
break |
|||
} |
|||
// Do something with the line of text |
|||
// in string variable s. |
|||
_ = s |
|||
} |
|||
}</syntaxhighlight> |
|||
Or, using <code>bufio.Scanner</code> you can read |
|||
line at a time, |
|||
word at a time, |
|||
byte or Unicode code point at a time, |
|||
or by any custom "split function". |
|||
<syntaxhighlight lang="go">package main |
|||
import ( |
|||
"bufio" |
|||
"log" |
|||
"os" |
|||
) |
|||
func main() { |
|||
s := bufio.NewScanner(os.Stdin) |
|||
// Select the split function, other ones are available |
|||
// in bufio or you can provide your own. |
|||
s.Split(bufio.ScanWords) |
|||
for s.Scan() { |
|||
// Get and use the next 'token' |
|||
asBytes := s.Bytes() // Bytes does no alloaction |
|||
asString := s.Text() // Text returns a newly allocated string |
|||
_, _ = asBytes, asString |
|||
} |
|||
if err := s.Err(); err != nil { |
|||
// Handle/report any error (EOF will not be reported) |
|||
log.Fatal(err) |
|||
} |
|||
}</syntaxhighlight> |
|||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
Solution: |
Solution: |
||
< |
<syntaxhighlight lang="groovy">def lineMap = [:] |
||
System.in.eachLine { line, i -> |
System.in.eachLine { line, i -> |
||
lineMap[i] = line |
lineMap[i] = line |
||
} |
} |
||
lineMap.each { println it }</ |
lineMap.each { println it }</syntaxhighlight> |
||
Test |
{{out|Test}} |
||
<pre>$ groovy -e 'def lineMap = [:] |
<pre>$ groovy -e 'def lineMap = [:] |
||
> System.in.eachLine { line, i -> |
> System.in.eachLine { line, i -> |
||
Line 833: | Line 1,620: | ||
> EOF</pre> |
> EOF</pre> |
||
{{out}} |
|||
Output: |
|||
<pre>1= |
<pre>1= |
||
2=Whose woods these are I think I know |
2=Whose woods these are I think I know |
||
Line 844: | Line 1,631: | ||
The whole contents of a file can be read lazily. The standard functions ''lines'' and ''words'' convert that lazily into the lists of lines resp. words. Usually, one wouldn't use extra routines for that, but just use ''readFile'' and then put 'lines' or ''words'' somewhere in the next processing step. |
The whole contents of a file can be read lazily. The standard functions ''lines'' and ''words'' convert that lazily into the lists of lines resp. words. Usually, one wouldn't use extra routines for that, but just use ''readFile'' and then put 'lines' or ''words'' somewhere in the next processing step. |
||
< |
<syntaxhighlight lang="haskell">import System.IO |
||
readLines :: Handle -> IO [String] |
readLines :: Handle -> IO [String] |
||
Line 854: | Line 1,641: | ||
readWords h = do |
readWords h = do |
||
s <- hGetContents h |
s <- hGetContents h |
||
return $ words s</ |
return $ words s</syntaxhighlight> |
||
=={{header|HicEst}}== |
=={{header|HicEst}}== |
||
< |
<syntaxhighlight lang="hicest">CHARACTER name='myfile.txt', string*1000 |
||
OPEN(FIle=name, OLD, LENgth=bytes, IOStat=errorcode, ERror=9) |
OPEN(FIle=name, OLD, LENgth=bytes, IOStat=errorcode, ERror=9) |
||
Line 866: | Line 1,653: | ||
ENDDO |
ENDDO |
||
9 WRITE(Messagebox, Name) line, errorcode</ |
9 WRITE(Messagebox, Name) line, errorcode</syntaxhighlight> |
||
=={{header|i}}== |
|||
<syntaxhighlight lang="i">software { |
|||
loop { |
|||
read() |
|||
errors { |
|||
exit |
|||
} |
|||
} |
|||
}</syntaxhighlight> |
|||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |
||
< |
<syntaxhighlight lang="icon">link str2toks |
||
# call either words or lines depending on what you want to do. |
# call either words or lines depending on what you want to do. |
||
procedure main() |
procedure main() |
||
Line 882: | Line 1,679: | ||
local line |
local line |
||
while line := read() do line ? every write(str2toks()) |
while line := read() do line ? every write(str2toks()) |
||
end</ |
end</syntaxhighlight> |
||
{{libheader|Icon Programming Library}} |
{{libheader|Icon Programming Library}} |
||
See [http://www.cs.arizona.edu/icon/library/src/procs/str2toks.icn str2toks] |
See [http://www.cs.arizona.edu/icon/library/src/procs/str2toks.icn str2toks] |
||
Line 888: | Line 1,685: | ||
=={{header|J}}== |
=={{header|J}}== |
||
Script "read-input-until-eof.ijs": |
Script "read-input-until-eof.ijs": |
||
<lang |
<syntaxhighlight lang="j">#!/usr/bin/ijconsole |
||
NB. read input until EOF |
NB. read input until EOF |
||
((1!:1) 3)(1!:2) 4 |
((1!:1) 3)(1!:2) 4 NB. tested under j602 |
||
exit ''</ |
exit ''</syntaxhighlight> |
||
Example: |
Example: |
||
< |
<syntaxhighlight lang="j">$ ./read-input-to-eof.ijs <<EOF |
||
> abc |
> abc |
||
> def |
> def |
||
Line 902: | Line 1,699: | ||
def |
def |
||
ghi |
ghi |
||
now is the time for all good men ...</ |
now is the time for all good men ...</syntaxhighlight> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
Some people prefer <tt>Scanner</tt> or <tt>BufferedReader</tt>, so a way with each is presented. |
Some people prefer <tt>Scanner</tt> or <tt>BufferedReader</tt>, so a way with each is presented. |
||
< |
<syntaxhighlight lang="java">import java.io.InputStream; |
||
import java.util.Scanner; |
|||
... |
|||
Scanner in = new Scanner(System.in);//stdin |
|||
public class InputLoop { |
|||
//new Scanner(new FileInputStream(filename)) for a file |
|||
public static void main(String args[]) { |
|||
//new Scanner(socket.getInputStream()) for a network stream |
|||
// To read from stdin: |
|||
while(in.hasNext()){ |
|||
InputStream source = System.in; |
|||
String input = in.next(); //in.nextLine() for line-by-line |
|||
//process the input here |
|||
/* |
|||
}</lang> |
|||
Or, to read from a file: |
|||
InputStream source = new FileInputStream(filename); |
|||
Or, to read from a network stream: |
|||
InputStream source = socket.getInputStream(); |
|||
*/ |
|||
Scanner in = new Scanner(source); |
|||
while(in.hasNext()){ |
|||
String input = in.next(); // Use in.nextLine() for line-by-line reading |
|||
// Process the input here. For example, you could print it out: |
|||
System.out.println(input); |
|||
} |
|||
} |
|||
}</syntaxhighlight> |
|||
Or |
Or |
||
< |
<syntaxhighlight lang="java">import java.io.BufferedReader; |
||
import java.io.IOException; |
import java.io.IOException; |
||
import java.io.InputStreamReader; |
import java.io.InputStreamReader; |
||
import java.io.Reader; |
|||
... |
|||
try{ |
|||
public class InputLoop { |
|||
BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));//stdin |
|||
public static void main(String args[]) { |
|||
//new BufferedReader(new FileReader(filename)) for a file |
|||
// To read from stdin: |
|||
//new BufferedReader(new InputStreamReader(socket.getInputStream())) for a network stream |
|||
Reader reader = new InputStreamReader(System.in); |
|||
while(inp.ready()){ |
|||
String input = inp.readLine();//line-by-line only |
|||
/* |
|||
//in.read() for character-by-character |
|||
Or, to read from a file: |
|||
//process the input here |
|||
Reader reader = new FileReader(filename); |
|||
} |
|||
} catch (IOException e) { |
|||
Or, to read from a network stream: |
|||
//There was an input error |
|||
Reader reader = new InputStreamReader(socket.getInputStream()); |
|||
}</lang> |
|||
*/ |
|||
try { |
|||
BufferedReader inp = new BufferedReader(reader); |
|||
while(inp.ready()) { |
|||
int input = inp.read(); // Use in.readLine() for line-by-line |
|||
// Process the input here. For example, you can print it out. |
|||
System.out.println(input); |
|||
} |
|||
} catch (IOException e) { |
|||
// There was an input error. |
|||
} |
|||
} |
|||
}</syntaxhighlight> |
|||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |
||
Line 942: | Line 1,771: | ||
As above, this operates on standard input |
As above, this operates on standard input |
||
< |
<syntaxhighlight lang="javascript">var text_stream = WScript.StdIn; |
||
var i = 0; |
var i = 0; |
||
Line 949: | Line 1,778: | ||
// do something with line |
// do something with line |
||
WScript.echo(++i + ": " + line); |
WScript.echo(++i + ": " + line); |
||
}</ |
}</syntaxhighlight> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
Line 956: | Line 1,785: | ||
. |
. |
||
For example, to echo each line of text in a file, one could invoke jq as follows:< |
For example, to echo each line of text in a file, one could invoke jq as follows:<syntaxhighlight lang="jq">jq -r -R . FILENAME |
||
</syntaxhighlight> |
|||
</lang> |
|||
If the input file consists of well-formed JSON entities (including scalars), then the following invocation could be used to "pretty-print" the input: <lang |
If the input file consists of well-formed JSON entities (including scalars), then the following invocation could be used to "pretty-print" the input: <syntaxhighlight lang="jq">jq . FILENAME</syntaxhighlight> |
||
Other options, e.g. to emit JSON in compact form, also exist. |
Other options, e.g. to emit JSON in compact form, also exist. |
||
=={{header|Jsish}}== |
|||
<syntaxhighlight lang="javascript">/* Input loop in Jsish */ |
|||
var line; |
|||
var cs = 0, ls = 0; |
|||
while (line = console.input()) { cs += line.length; ls += 1; } |
|||
printf("%d lines, %d characters\n", ls, cs);</syntaxhighlight> |
|||
{{out}} |
|||
<pre>prompt$ jsish input-loop.jsi <input-loop.jsi |
|||
8 lines, 159 characters</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
We create a text stream and read the lines from the stream one by one, printing them on screen. |
We create a text stream and read the lines from the stream one by one, printing them on screen. |
||
Note that the lines end by a newline, except the last one. The ending newlines are part of the strings returned by the function readline. Once the end of the stream is reached, readline returns an empty string. |
Note that the lines end by a newline, except the last one. The ending newlines are part of the strings returned by the function readline. Once the end of the stream is reached, readline returns an empty string. |
||
< |
<syntaxhighlight lang="julia">stream = IOBuffer("1\n2\n3\n4\n\n6") |
||
while |
while !eof(stream) |
||
line = readline(stream) |
|||
println(line) |
|||
end</lang> |
|||
end</syntaxhighlight> |
|||
{{Out}} |
{{Out}} |
||
<pre>1 |
<pre>1 |
||
Line 977: | Line 1,822: | ||
6</pre> |
6</pre> |
||
=={{header|Kotlin}}== |
|||
<syntaxhighlight lang="scala">// version 1.1 |
|||
import java.util.* |
|||
fun main(args: Array<String>) { |
|||
println("Keep entering text or the word 'quit' to end the program:") |
|||
val sc = Scanner(System.`in`) |
|||
val words = mutableListOf<String>() |
|||
while (true) { |
|||
val input: String = sc.next() |
|||
if (input.trim().toLowerCase() == "quit") { |
|||
if (words.size > 0) println("\nYou entered the following words:\n${words.joinToString("\n")}") |
|||
return |
|||
} |
|||
words.add(input) |
|||
} |
|||
}</syntaxhighlight> |
|||
Sample input/output: |
|||
{{out}} |
|||
<pre> |
|||
Keep entering text or the word 'quit' to end the program: |
|||
The quick brown fox |
|||
jumps over the lazy dog |
|||
quit |
|||
You entered the following words: |
|||
The |
|||
quick |
|||
brown |
|||
fox |
|||
jumps |
|||
over |
|||
the |
|||
lazy |
|||
dog |
|||
</pre> |
|||
=={{header|Lang5}}== |
=={{header|Lang5}}== |
||
< |
<syntaxhighlight lang="lang5">: read-lines do read . "\n" . eof if break then loop ; |
||
: ==>contents |
: ==>contents |
||
'< swap open 'fh set fh fin read-lines fh close ; |
'< swap open 'fh set fh fin read-lines fh close ; |
||
'file.txt ==>contents</ |
'file.txt ==>contents</syntaxhighlight> |
||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
<syntaxhighlight lang="lasso"> |
|||
<lang Lasso> |
|||
local( |
local( |
||
myfile = file('//path/to/file.txt'), |
myfile = file('//path/to/file.txt'), |
||
Line 996: | Line 1,879: | ||
} |
} |
||
#textresult -> join('<br />')</ |
#textresult -> join('<br />')</syntaxhighlight> |
||
Result: |
Result: |
||
Line 1,004: | Line 1,887: | ||
=={{header|Liberty BASIC}}== |
=={{header|Liberty BASIC}}== |
||
<syntaxhighlight lang="lb"> |
|||
<lang lb> |
|||
filedialog "Open","*.txt",file$ |
filedialog "Open","*.txt",file$ |
||
if file$="" then end |
if file$="" then end |
||
Line 1,013: | Line 1,896: | ||
wend |
wend |
||
close #f |
close #f |
||
end </ |
end </syntaxhighlight> |
||
=={{header|LIL}}== |
|||
From the canread.lil sample that ships with LIL. |
|||
<syntaxhighlight lang="tcl"># |
|||
# canread test (note that canread is not available in LIL/FPLIL itself |
|||
# but provided by the command line interfaces in main.c/lil.pas) |
|||
# |
|||
# You can either call this and enter lines directly (use Ctrl+Z/Ctrl+D |
|||
# to finish) or a redirect (e.g. lil canread.lil < somefile.txt) |
|||
# |
|||
# Normally this is how you are supposed to read multiple lines from |
|||
# the standard input using the lil executable. For an alternative way |
|||
# that uses error catching via try see the eoferror.lil script. |
|||
# |
|||
set count 0 |
|||
while {[canread]} { |
|||
readline |
|||
inc count |
|||
} |
|||
print $count lines</syntaxhighlight> |
|||
{{out}} |
|||
<pre>prompt$ lil canread.lil <canread.lil |
|||
19 lines</pre> |
|||
=={{header|Logo}}== |
=={{header|Logo}}== |
||
Line 1,020: | Line 1,929: | ||
* readword - returns a line as a single word, or an empty list if it reached the end of file |
* readword - returns a line as a single word, or an empty list if it reached the end of file |
||
* readrawline - returns a line as a single word, with no characters escaped |
* readrawline - returns a line as a single word, with no characters escaped |
||
< |
<syntaxhighlight lang="logo">while [not eof?] [print readline]</syntaxhighlight> |
||
=={{header|LSL}}== |
=={{header|LSL}}== |
||
Line 1,026: | Line 1,935: | ||
To test it yourself; rez a box on the ground, add the following as a New Script, create a notecard named "Input_Loop_Data_Source.txt", and put what ever data you want in it (in this case I just put a copy of the source code.) |
To test it yourself; rez a box on the ground, add the following as a New Script, create a notecard named "Input_Loop_Data_Source.txt", and put what ever data you want in it (in this case I just put a copy of the source code.) |
||
< |
<syntaxhighlight lang="lsl">string sNOTECARD = "Input_Loop_Data_Source.txt"; |
||
default { |
default { |
||
integer iNotecardLine = 0; |
integer iNotecardLine = 0; |
||
Line 1,041: | Line 1,950: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
|||
Output: |
|||
<pre>Reading 'Input_Loop_Data_Source.txt' |
<pre>Reading 'Input_Loop_Data_Source.txt' |
||
0: string sNOTECARD = "Input_Loop_Data_Source.txt"; |
0: string sNOTECARD = "Input_Loop_Data_Source.txt"; |
||
Line 1,063: | Line 1,972: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
< |
<syntaxhighlight lang="lua">lines = {} |
||
str = io.read() |
str = io.read() |
||
while str do |
while str do |
||
table.insert(lines,str) |
table.insert(lines,str) |
||
str = io.read() |
str = io.read() |
||
end</ |
end</syntaxhighlight> |
||
=== Via generic for loop === |
=== Via generic for loop === |
||
Reads line-by-line via an iterator (from stdin). Substitute <code>io.lines()</code> with <code>io.open(filename, "r"):lines()</code> to read from a file. |
Reads line-by-line via an iterator (from stdin). Substitute <code>io.lines()</code> with <code>io.open(filename, "r"):lines()</code> to read from a file. |
||
< |
<syntaxhighlight lang="lua">lines = {} |
||
for line in io.lines() do |
for line in io.lines() do |
||
table.insert(lines, line) -- add the line to the list of lines |
table.insert(lines, line) -- add the line to the list of lines |
||
end</ |
end</syntaxhighlight> |
||
=={{header| |
=={{header|M2000 Interpreter}}== |
||
<syntaxhighlight lang="m2000 interpreter"> |
|||
<lang Mathematica>stream = OpenRead["file.txt"]; |
|||
Document A$={1st Line |
|||
2nd line |
|||
3rd line |
|||
} |
|||
Save.Doc A$, "test.txt", 0 ' 0 for utf16-le |
|||
\\ we use Wide for utf16-le \\ without it we open using ANSI |
|||
Open "test.txt" For Wide Input Exclusive as #N |
|||
While Not Eof(#N) { |
|||
Line Input #N, ThisLine$ |
|||
Print ThisLine$ |
|||
} |
|||
Close #N |
|||
Clear A$ |
|||
Load.Doc A$, "test.txt" |
|||
\\ print proportional text, all lines |
|||
Report A$ |
|||
\\ Print one line, non proportional |
|||
\\ using paragraphs |
|||
For i=0 to Doc.par(A$)-1 |
|||
Print Paragraph$(A$, i) |
|||
Next i |
|||
\\ List of current variables (in any scope, public only) |
|||
List |
|||
</syntaxhighlight> |
|||
=={{header|Maple}}== |
|||
<syntaxhighlight lang="maple"> |
|||
readinput:=proc(filename) |
|||
local line,file; |
|||
file:=""; |
|||
line:=readline(filename); |
|||
while line<>0 do |
|||
line:=readline(filename); |
|||
file:=cat(file,line); |
|||
end do; |
|||
end proc; |
|||
</syntaxhighlight> |
|||
=={{header|MACRO-10}}== |
|||
<syntaxhighlight lang="macro-10"> |
|||
TITLE Input Loop |
|||
COMMENT ! |
|||
Input-loop example, PDP-10 assembly language, kjx 2022. |
|||
Assembler: MACRO-10 Operating system: TOPS-20 |
|||
This program opens the file "test.txt" and prints it's contents. |
|||
Note that the PDP-10 is a word-addressable machine with 36bit words, |
|||
and text-files use 7bit ASCII. That means one word fits 5 characters |
|||
and the buffer length of 20 (octal!) makes room for 80 (decimal) char- |
|||
acters. |
|||
The system-call used, SIN%, can also read until a specific character |
|||
(like CR) is seen, so reading one-line-at-a-time is also possible with |
|||
minimal changes. |
|||
! |
|||
SEARCH MONSYM,MACSYM |
|||
.REQUIRE SYS:MACREL |
|||
STDAC. ;Define standard register names. |
|||
;; |
|||
;; Buffers and data: |
|||
;; |
|||
PDLLEN==20 |
|||
PDL: BLOCK PDLLEN ;Callstack (for ERCAL later). |
|||
MAXBUF==20 ;Use 20 36bit words for buffer. |
|||
MAXCHR==MAXBUF*5 ;One 36bit words fits 5 7bit bytes. |
|||
IN.BUF: BLOCK MAXBUF ;Buffer for lines read from file. |
|||
IN.JFN: BLOCK 1 ;Space for file-descriptor (JFN) |
|||
;; |
|||
;; Execution begins here: |
|||
;; |
|||
GO:: RESET% ;Initialize process. |
|||
MOVE P,[IOWD PDLLEN,PDL] ;Initialize stack. |
|||
;; |
|||
;; To open a file, we first have to get a file-handle (JFN): |
|||
;; |
|||
MOVX T1,GJ%OLD+GJ%SHT ;Flags go into T1: |
|||
; GJ%OLD = File already exists. |
|||
; GJ%SHT = Short system call. |
|||
HRROI T2,[ASCIZ /test.txt/] ;File name goes to T2. |
|||
GTJFN% ;Get a JFN for this file. |
|||
ERJMPS ERROR ; On error jump to ERROR. |
|||
MOVEM T1,IN.JFN ;Save the JFN. |
|||
;; |
|||
;; Now we can open the file: |
|||
;; |
|||
HRRZ T1,IN.JFN ;Right half-word of JFN into T1 + |
|||
;zero out left half to get rid of |
|||
;flags returned by GTJFN%. |
|||
MOVX T2,FLD(7,OF%BSZ)+OF%RD ;7bit characters, read-only. |
|||
OPENF% ;Open file. |
|||
ERJMPS ERROR ; On error jump to ERROR. |
|||
;; |
|||
;; Now we're reading MAXCHR 7bit bytes from that file |
|||
;; into IN.BUF and print that. If we'll see EOF, Q1 is |
|||
;; set to 1, and the loop is exited. |
|||
;; |
|||
SETZ Q1 ;Clear Q1 (we use that as an EOF marker) |
|||
DO. |
|||
MOVE T1,IN.JFN ;Read from IN.JFN (opened file) |
|||
HRROI T2,IN.BUF ;and write into IN.BUF. |
|||
MOVNI T3,MAXCHR ;Read maximum of MAXCHR characters. |
|||
SIN% ;Read string from file. |
|||
ERCAL [ MOVE T1,IN.JFN ; Error occured... |
|||
GTSTS% ; Get file status. |
|||
TXNN T2,GS%EOF ; End-of-file reached? |
|||
JRST ERROR ; No: Something else happened... |
|||
MOVEI Q1,1 ; Yes: Set Q1 to 1. |
|||
RET ] ; Continue. |
|||
MOVEI T1,.PRIOU ;Write to standard output. |
|||
HRROI T2,IN.BUF ;Buffer to write. |
|||
ADDI T3,MAXCHR ;Substract chars read from |
|||
MOVN T3,T3 ;maximum buffer length. |
|||
SOUT% ;Print string. |
|||
CAIN Q1,1 ;Is Q1 == 1? |
|||
JRST ENDPRG ; Yes: EOF was found above. |
|||
LOOP. ;Q1 is 0, so no EOF, continue. |
|||
ENDDO. |
|||
;; |
|||
;; ENDPRG: Close file and halt. |
|||
;; |
|||
ENDPRG: HRRZ T1,IN.JFN ;Put JFN into T1. |
|||
CLOSF% ;Close that file. |
|||
ERJMPS ERROR ; Go print error-msgs on error. |
|||
HALTF% ;Halt program. |
|||
JRST GO ;Allow for 'continue'-command. |
|||
;; |
|||
;; ERROR: Print standardized error-message and halt. |
|||
;; |
|||
ERROR: MOVEI T1,.PRIOU ;Print error on terminal (.PRIOU) |
|||
MOVE T2,[.FHSLF,,-1] ;Own process, most recent error. |
|||
SETZ T3, ;No limit on size of message. |
|||
ERSTR% ;Print that error message. |
|||
JFCL ; Ignore errors from ERSTR. |
|||
JFCL ; dito. |
|||
HALTF% ;Stop program. |
|||
JRST GO ;Allow for continuation. |
|||
END GO |
|||
</syntaxhighlight> |
|||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
|||
<syntaxhighlight lang="mathematica">stream = OpenRead["file.txt"]; |
|||
While[a != EndOfFile, Read[stream, Word]]; |
While[a != EndOfFile, Read[stream, Word]]; |
||
Close[stream]</ |
Close[stream]</syntaxhighlight> |
||
=={{header|MAXScript}}== |
=={{header|MAXScript}}== |
||
this function will read a file line by line. |
this function will read a file line by line. |
||
< |
<syntaxhighlight lang="maxscript">fn ReadAFile FileName = |
||
( |
( |
||
local in_file = openfile FileName |
local in_file = openfile FileName |
||
Line 1,095: | Line 2,166: | ||
) |
) |
||
close in_file |
close in_file |
||
)</ |
)</syntaxhighlight> |
||
=={{header|Mercury}}== |
=={{header|Mercury}}== |
||
<lang> |
<syntaxhighlight lang="text"> |
||
:- module input_loop. |
:- module input_loop. |
||
:- interface. |
:- interface. |
||
Line 1,130: | Line 2,201: | ||
io.set_exit_status(1, !IO) |
io.set_exit_status(1, !IO) |
||
). |
). |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|mIRC Scripting Language}}== |
=={{header|mIRC Scripting Language}}== |
||
< |
<syntaxhighlight lang="mirc">var %n = 1 |
||
while (%n <= $lines(input.txt)) { |
while (%n <= $lines(input.txt)) { |
||
write output.txt $read(input.txt,%n) |
write output.txt $read(input.txt,%n) |
||
inc %n |
inc %n |
||
}</ |
}</syntaxhighlight> |
||
=={{header|ML/I}}== |
=={{header|ML/I}}== |
||
The very nature of ML/I is that its default behaviour |
The very nature of ML/I is that its default behaviour |
||
is to copy from input to output until it reaches end of file. |
|||
<lang ML/I></lang> |
|||
=={{header|Modula-2}}== |
=={{header|Modula-2}}== |
||
< |
<syntaxhighlight lang="modula2">PROCEDURE ReadName (VAR str : ARRAY OF CHAR); |
||
VAR n : CARDINAL; |
VAR n : CARDINAL; |
||
Line 1,166: | Line 2,237: | ||
IF n <= HIGH (str) THEN str [n-1] := 0C END; |
IF n <= HIGH (str) THEN str [n-1] := 0C END; |
||
lastCh := ch |
lastCh := ch |
||
END ReadName;</ |
END ReadName;</syntaxhighlight> |
||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
< |
<syntaxhighlight lang="modula3">MODULE Output EXPORTS Main; |
||
IMPORT Rd, Wr, Stdio; |
IMPORT Rd, Wr, Stdio; |
||
Line 1,184: | Line 2,253: | ||
Wr.PutText(Stdio.stdout, buf); |
Wr.PutText(Stdio.stdout, buf); |
||
END; |
END; |
||
END Output.</ |
END Output.</syntaxhighlight> |
||
=={{header|NetRexx}}== |
=={{header|NetRexx}}== |
||
=== Using NetRexx <tt>ASK</tt> Special Variable === |
=== Using NetRexx <tt>ASK</tt> Special Variable === |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref symbols nobinary |
options replace format comments java crossref symbols nobinary |
||
Line 1,198: | Line 2,267: | ||
loop label ioloop forever |
loop label ioloop forever |
||
inLine = ask |
inLine = ask |
||
if inLine = null then leave ioloop -- stop on EOF |
if inLine = null then leave ioloop -- stop on EOF (Try Ctrl-D on UNIX-like systems or Ctrl-Z on Windows) |
||
lineNo = lineNo + 1 |
lineNo = lineNo + 1 |
||
lines[0] = lineNo |
lines[0] = lineNo |
||
Line 1,209: | Line 2,278: | ||
return |
return |
||
</syntaxhighlight> |
|||
</lang> |
|||
=== Using Java <tt>Scanner</tt> === |
=== Using Java <tt>Scanner</tt> === |
||
< |
<syntaxhighlight lang="netrexx">/* NetRexx */ |
||
options replace format comments java crossref symbols nobinary |
options replace format comments java crossref symbols nobinary |
||
Line 1,232: | Line 2,301: | ||
return |
return |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Nim}}== |
|||
Read from stdin until end of data has been reached: |
|||
<syntaxhighlight lang="nim">var line = "" |
|||
while stdin.readLine(line): |
|||
echo line</syntaxhighlight> |
|||
Choose splitting string: |
|||
<syntaxhighlight lang="nim">import strutils |
|||
var lines = stdin.readAll() |
|||
for line in lines.split("\n"): |
|||
echo line</syntaxhighlight> |
|||
Read from a known text file: |
|||
=={{header|Nimrod}}== |
|||
< |
<syntaxhighlight lang="nim">var i = open("input.txt") |
||
for line in i.lines: |
for line in i.lines: |
||
discard # process line |
discard # process line |
||
i.close()</ |
i.close()</syntaxhighlight> |
||
Another, shorter, way to do this: |
|||
<syntaxhighlight lang="nim">for line in "input.text".lines: |
|||
discard # process line</syntaxhighlight> |
|||
=={{header|NodeJS}}== |
|||
<syntaxhighlight lang="nodejs"> |
|||
#!/usr/bin/env node |
|||
const EventEmitter = require('events'); |
|||
function stdinLineByLine() { |
|||
const stdin = new EventEmitter(); |
|||
let buff = ''; |
|||
process.stdin |
|||
.on('data', data => { |
|||
buff += data; |
|||
lines = buff.split(/\r\n|\n/); |
|||
buff = lines.pop(); |
|||
lines.forEach(line => stdin.emit('line', line)); |
|||
}) |
|||
.on('end', () => { |
|||
if (buff.length > 0) stdin.emit('line', buff); |
|||
}); |
|||
return stdin; |
|||
} |
|||
const stdin = stdinLineByLine(); |
|||
stdin.on('line', console.log); |
|||
</syntaxhighlight> |
|||
=={{header|Oberon-2}}== |
|||
Works with oo2c Version 2 |
|||
<syntaxhighlight lang="oberon2"> |
|||
MODULE InputLoop; |
|||
IMPORT |
|||
StdChannels, |
|||
Channel; |
|||
VAR |
|||
reader: Channel.Reader; |
|||
writer: Channel.Writer; |
|||
c: CHAR; |
|||
BEGIN |
|||
reader := StdChannels.stdin.NewReader(); |
|||
writer := StdChannels.stdout.NewWriter(); |
|||
reader.ReadByte(c); |
|||
WHILE reader.res = Channel.done DO |
|||
writer.WriteByte(c); |
|||
reader.ReadByte(c) |
|||
END |
|||
END InputLoop. |
|||
</syntaxhighlight> |
|||
Execute: InputLoop < Inputloop.Mod<br/> |
|||
Output: |
|||
<pre> |
|||
MODULE InputLoop; |
|||
IMPORT |
|||
StdChannels, |
|||
Channel; |
|||
VAR |
|||
reader: Channel.Reader; |
|||
writer: Channel.Writer; |
|||
c: CHAR; |
|||
BEGIN |
|||
reader := StdChannels.stdin.NewReader(); |
|||
writer := StdChannels.stdout.NewWriter(); |
|||
reader.ReadByte(c); |
|||
WHILE reader.res = Channel.done DO |
|||
writer.WriteByte(c); |
|||
reader.ReadByte(c) |
|||
END |
|||
END InputLoop. |
|||
</pre> |
|||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang="objeck"> |
||
use IO; |
use IO; |
||
Line 1,259: | Line 2,417: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|OCaml}}== |
=={{header|OCaml}}== |
||
< |
<syntaxhighlight lang="ocaml">let rec read_lines ic = |
||
try |
|||
try let line = input_line ic in |
|||
let line = input_line ic in |
|||
line :: read_lines ic |
line :: read_lines ic |
||
with End_of_file -> |
with End_of_file -> |
||
[]</ |
[]</syntaxhighlight> |
||
The version above will work for small files, but it is not tail-recursive. <br /> |
The version above will work for small files, but it is not tail-recursive. <br /> |
||
Below will be more scalable: |
Below will be more scalable: |
||
< |
<syntaxhighlight lang="ocaml">let read_line_opt ic = |
||
try Some (input_line ic) |
try Some (input_line ic) |
||
with End_of_file -> None |
with End_of_file -> None |
||
Line 1,277: | Line 2,436: | ||
let read_lines ic = |
let read_lines ic = |
||
let rec loop acc = |
let rec loop acc = |
||
match |
match read_line_opt ic with |
||
| Some line -> loop (line :: acc) |
| Some line -> loop (line :: acc) |
||
| None -> List.rev acc |
| None -> (List.rev acc) |
||
in |
in |
||
loop [] |
loop [] |
||
;;</ |
;;</syntaxhighlight> |
||
Or with a higher order function: |
Or with a higher order function: |
||
< |
<syntaxhighlight lang="ocaml">let read_lines f ic = |
||
let rec loop () = |
let rec loop () = |
||
try f(input_line ic); loop() |
try f (input_line ic); loop () |
||
with End_of_file -> () |
with End_of_file -> () |
||
in |
in |
||
loop() |
loop () |
||
let () = |
|||
read_lines print_endline (open_in Sys.argv.(1))</lang> |
|||
let ic = open_in Sys.argv.(1) in |
|||
read_lines print_endline ic</syntaxhighlight> |
|||
=={{header|Oforth}}== |
|||
Reads a file line by line and write each line on standard output : |
|||
<syntaxhighlight lang="oforth">: readFile(filename) File new(filename) apply(#println) ; </syntaxhighlight> |
|||
=={{header|OmniMark}}== |
|||
This OmniMark script will output the file text.txt verbatim, reading it one line at a time: |
|||
<syntaxhighlight lang="omnimark"> |
|||
;command line: omnimark -sb Input-loop.xom text.txt |
|||
process |
|||
repeat scan file #args |
|||
match any-text+ => line white-space |
|||
output '%x(line)%n' |
|||
again |
|||
</syntaxhighlight> |
|||
That would be less common than using #main-input and find rule(s), noting the following effectively produces the same result: |
|||
<syntaxhighlight lang="omnimark"> |
|||
;command line: omnimark -sb Input-loop.xom text.txt |
|||
process |
|||
submit #main-input |
|||
find any-text+ => line |
|||
output line |
|||
</syntaxhighlight> |
|||
=={{header|Oz}}== |
=={{header|Oz}}== |
||
< |
<syntaxhighlight lang="oz">%% Returns a list of lines. |
||
%% Text: an instance of Open.text (a mixin class) |
%% Text: an instance of Open.text (a mixin class) |
||
fun {ReadAll Text} |
fun {ReadAll Text} |
||
Line 1,302: | Line 2,493: | ||
[] Line then Line|{ReadAll Text} |
[] Line then Line|{ReadAll Text} |
||
end |
end |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
< |
<syntaxhighlight lang="pascal">{ for stdio } |
||
var |
var |
||
Line 1,337: | Line 2,528: | ||
closefile(f); |
closefile(f); |
||
end;</ |
end;</syntaxhighlight> |
||
=={{header|PascalABC.NET}}== |
|||
<syntaxhighlight lang="delphi"> |
|||
begin |
|||
var f := OpenRead('a.txt'); |
|||
while not f.Eof do |
|||
Println(f.ReadString); |
|||
f.Close; |
|||
end. |
|||
</syntaxhighlight> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
The angle brackets operator ( <tt><...></tt> ) reads one line at a time from a filehandle in scalar context: |
The angle brackets operator ( <tt><...></tt> ) reads one line at a time from a filehandle in scalar context: |
||
< |
<syntaxhighlight lang="perl">open FH, "< $filename" or die "can't open file: $!"; |
||
while (my $line = <FH>) { |
while (my $line = <FH>) { |
||
chomp $line; # removes trailing newline |
chomp $line; # removes trailing newline |
||
# process $line |
# process $line |
||
} |
} |
||
close FH or die "can't close file: $!";</ |
close FH or die "can't close file: $!";</syntaxhighlight> |
||
Or you can get a list of all lines when you use it in list context: |
Or you can get a list of all lines when you use it in list context: |
||
< |
<syntaxhighlight lang="perl">@lines = <FH>;</syntaxhighlight> |
||
Or a simpler program for lines of files entered as command line arguments or standard input: |
Or a simpler program for lines of files entered as command line arguments or standard input: |
||
< |
<syntaxhighlight lang="perl">while (<>) { |
||
# $_ contains a line |
# $_ contains a line |
||
}</ |
}</syntaxhighlight> |
||
Invoking perl with the -p or -n option implies the above loop, executing its code once per input line, with the line stored in $_. -p will print $_ automatically at the end of each iteration, -n will not. |
Invoking perl with the -p or -n option implies the above loop, executing its code once per input line, with the line stored in $_. -p will print $_ automatically at the end of each iteration, -n will not. |
||
Line 1,371: | Line 2,573: | ||
Hello 5</pre> |
Hello 5</pre> |
||
=={{header| |
=={{header|Phix}}== |
||
{{trans|Euphoria}} |
|||
<lang perl6>my $handle = open "filename.txt"; # $handle could be $*IN to read from standard input |
|||
Process text stream line-by-line: |
|||
<!--<syntaxhighlight lang="phix">(notonline)--> |
|||
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (file i/o)</span> |
|||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">process_line_by_line</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #004080;">object</span> <span style="color: #000000;">line</span> |
|||
<span style="color: #008080;">while</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span> |
|||
<span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">gets</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #008080;">if</span> <span style="color: #004080;">atom</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> |
|||
<span style="color: #008080;">exit</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span> |
|||
<span style="color: #000080;font-style:italic;">-- process the line</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span> |
|||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span> |
|||
<!--</syntaxhighlight>--> |
|||
=={{header|Phixmonti}}== |
|||
for $handle.lines -> $line { # iterates the lines of the $handle |
|||
<syntaxhighlight lang="Phixmonti">/# Rosetta Code problem: http://rosettacode.org/wiki/Input_loop |
|||
by Galileo, 10/2022 #/ |
|||
include ..\Utilitys.pmt |
|||
# line endings are automatically stripped |
|||
def eof? dup -1 == enddef |
|||
for $line.words -> $word { # iterates the words of the line |
|||
# are considered words groups of non-whitespace characters |
|||
"input.txt" "r" fopen |
|||
} |
|||
( inf 0 -1 ) for drop |
|||
} |
|||
dup fgets eof? if drop exitfor else print endif |
|||
</lang> |
|||
endfor |
|||
fclose |
|||
"input.txt" "r" fopen |
|||
eof? not while |
|||
dup fgets eof? if drop false else print true endif |
|||
endwhile |
|||
fclose</syntaxhighlight> |
|||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php">$fh = fopen($filename, 'r'); |
||
if ($fh) { |
if ($fh) { |
||
while (!feof($fh)) { |
while (!feof($fh)) { |
||
Line 1,392: | Line 2,619: | ||
} |
} |
||
fclose($fh); |
fclose($fh); |
||
}</ |
}</syntaxhighlight> |
||
Or you can get an array of all the lines in the file: |
Or you can get an array of all the lines in the file: |
||
< |
<syntaxhighlight lang="php">$lines = file($filename);</syntaxhighlight> |
||
Or you can get the entire file as a string: |
Or you can get the entire file as a string: |
||
< |
<syntaxhighlight lang="php">$contents = file_get_contents($filename);</syntaxhighlight> |
||
=={{header|Picat}}== |
|||
<syntaxhighlight lang="picat">main => |
|||
Reader = open("file.txt"), |
|||
while(not at_end_of_stream(Reader)) |
|||
L = read_line(Reader), |
|||
println(L) |
|||
end, |
|||
close(Reader).</syntaxhighlight> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
This reads all lines in a file, and returns them as a list of lists |
This reads all lines in a file, and returns them as a list of lists |
||
< |
<syntaxhighlight lang="picolisp">(in "file.txt" |
||
(make |
(make |
||
(until (eof) |
(until (eof) |
||
(link (line)) ) ) )</ |
(link (line)) ) ) )</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang="pli">declare line character (200) varying; |
||
open file (in) title ('/TEXT.DAT,type(text),recsize(200)' ); |
open file (in) title ('/TEXT.DAT,type(text),recsize(200)' ); |
||
Line 1,414: | Line 2,651: | ||
do forever; |
do forever; |
||
get edit (line) (L); |
get file(in) edit (line) (L); |
||
put skip list (line); |
put skip list (line); |
||
end;</ |
end;</syntaxhighlight> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
< |
<syntaxhighlight lang="powershell">Get-Content c:\file.txt | |
||
ForEach-Object { |
ForEach-Object { |
||
$_ |
$_ |
||
}</ |
}</syntaxhighlight> |
||
or |
or |
||
< |
<syntaxhighlight lang="powershell">ForEach-Object -inputobject (get-content c:\file.txt) {$_}</syntaxhighlight> |
||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
File objects can be read bytewise, characterwise (ASCII or UNICODE), floatwise, doublewise, integerwise, linewise ... |
File objects can be read bytewise, characterwise (ASCII or UNICODE), floatwise, doublewise, integerwise, linewise ... |
||
< |
<syntaxhighlight lang="purebasic">If OpenConsole() |
||
; file based line wise |
; file based line wise |
||
If ReadFile(0, "Text.txt") |
If ReadFile(0, "Text.txt") |
||
Line 1,444: | Line 2,681: | ||
CloseFile(1) |
CloseFile(1) |
||
EndIf |
EndIf |
||
EndIf</ |
EndIf</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
To create a Python3 input loop use python's `input()` function. |
|||
<syntaxhighlight lang="python">while(True): |
|||
x = input("What is your age? ") |
|||
print(x)</syntaxhighlight> |
|||
Python file objects can be iterated like lists: |
Python file objects can be iterated like lists: |
||
< |
<syntaxhighlight lang="python">my_file = open(filename, 'r') |
||
try: |
try: |
||
for line in my_file: |
for line in my_file: |
||
pass # process line, includes newline |
pass # process line, includes newline |
||
finally: |
finally: |
||
my_file.close()</ |
my_file.close()</syntaxhighlight> |
||
One can open a new stream for read and have it automatically close when done, with a new "with" statement: |
One can open a new stream for read and have it automatically close when done, with a new "with" statement: |
||
< |
<syntaxhighlight lang="python">#from __future__ import with_statement # is not needed in Python 3.6 |
||
with open(filename, 'r') as f: |
with open(filename, 'r') as f: |
||
for line in f: |
for line in f: |
||
pass # process line, includes newline</ |
pass # process line, includes newline</syntaxhighlight> |
||
You can also get lines manually from a file: |
You can also get lines manually from a file: |
||
< |
<syntaxhighlight lang="python">line = my_file.readline() # returns a line from the file |
||
lines = my_file.readlines() # returns a list of the rest of the lines from the file</ |
lines = my_file.readlines() # returns a list of the rest of the lines from the file</syntaxhighlight> |
||
This does not mix well with the iteration, however. |
This does not mix well with the iteration, however. |
||
When (multiple) filenames are given on the command line: |
When you want to read from stdin, or (multiple) filenames are given on the command line: |
||
< |
<syntaxhighlight lang="python">import fileinput |
||
for line in fileinput.input(): |
for line in fileinput.input(): |
||
pass # process line, includes newline</ |
pass # process line, includes newline</syntaxhighlight> |
||
The fileinput module can also do inplace file editing, follow line counts, and the name of the current file being read etc. |
The fileinput module can also do inplace file editing, follow line counts, and the name of the current file being read etc. |
||
=={{header|R}}== |
=={{header|R}}== |
||
Note that read.csv and read.table provide alternatives for files with 'dataset' style contents. |
Note that read.csv and read.table provide alternatives for files with 'dataset' style contents. |
||
< |
<syntaxhighlight lang="rsplus">lines <- readLines("file.txt")</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
The following prints input lines from standard input to standard output: |
The following prints input lines from standard input to standard output: |
||
<syntaxhighlight lang="racket"> |
|||
<lang racket> |
|||
#lang racket |
#lang racket |
||
(copy-port (current-input-port) (current-output-port)) |
(copy-port (current-input-port) (current-output-port)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Rapira}}== |
|||
<syntaxhighlight lang="rapira">while 1 do |
|||
input text: str |
|||
output: str |
|||
od</syntaxhighlight> |
|||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
In Raku, filehandles etc. provide the <code>.lines</code> and <code>.words</code> methods which return lazy lists, and can thus they be iterated using a <code>for</code> loop... |
|||
'''Line-by-line''' <small>''(line endings are automatically stripped)''</small> |
|||
*From a file:<syntaxhighlight lang="raku" line>for "filename.txt".IO.lines -> $line { |
|||
... |
|||
}</syntaxhighlight> |
|||
*From standard input:<syntaxhighlight lang="raku" line>for $*IN.lines -> $line { |
|||
... |
|||
}</syntaxhighlight> |
|||
*From a pipe:<syntaxhighlight lang="raku" line>for run(«find -iname *.txt», :out).out.lines -> $filename { |
|||
... |
|||
}</syntaxhighlight> |
|||
*From a pipe, with custom line separator <small>''(in this example to handle filenames containing newlines)''</small>:<syntaxhighlight lang="raku" line>for run(«find -iname *.txt -print0», :nl«\0», :out).out.lines -> $filename { |
|||
... |
|||
}</syntaxhighlight> |
|||
'''Word-by-word''' |
|||
*From a file <syntaxhighlight lang="raku" line>for "filename.txt".IO.words -> $word { |
|||
... |
|||
}</syntaxhighlight> |
|||
*From standard input or a pipe, accordingly. |
|||
=={{header|REBOL}}== |
=={{header|REBOL}}== |
||
< |
<syntaxhighlight lang="rebol">REBOL [ |
||
Title: "Basic Input Loop" |
Title: "Basic Input Loop" |
||
Author: oofoe |
|||
Date: 2009-12-06 |
|||
URL: http://rosettacode.org/wiki/Basic_input_loop |
URL: http://rosettacode.org/wiki/Basic_input_loop |
||
] |
] |
||
Line 1,511: | Line 2,784: | ||
f: next f ; Advance to next line. |
f: next f ; Advance to next line. |
||
] |
] |
||
close f</ |
close f</syntaxhighlight> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===version |
===version 1a=== |
||
{{works with|oorexx}} |
{{works with|oorexx and Regina}} |
||
Reading line by line from the standard input using <tt>linein</tt> and <tt>lines</tt> did not work. |
Reading line by line from the standard input using <tt>linein</tt> and <tt>lines</tt> did not work. |
||
< |
<syntaxhighlight lang="rexx">do while stream(stdin, "State") <> "NOTREADY" |
||
call charout ,charin(stdin) |
call charout ,charin(stdin) |
||
end</ |
end</syntaxhighlight> |
||
===version 1b=== |
|||
{{works with|oorexx and Regina}} |
|||
Apparently only lines() does not work |
|||
<syntaxhighlight lang="rexx">Do Until input='' |
|||
input=linein(stdin) |
|||
Call lineout ,input |
|||
End</syntaxhighlight> |
|||
===version 2=== |
===version 2=== |
||
{{works with|ARexx}} |
{{works with|ARexx}} |
||
< |
<syntaxhighlight lang="rexx">/* -- AREXX -- */ |
||
do until eof(stdin) |
do until eof(stdin) |
||
l = readln(stdin) |
l = readln(stdin) |
||
say l |
say l |
||
end</ |
end</syntaxhighlight> |
||
===version 3=== |
===version 3=== |
||
Note that if REXX is reading from the default (console) input stream, there is no well-<br>defined e-o-f (end of file), so to speak. |
Note that if REXX is reading from the default (console) input stream, there is no well-<br>defined e-o-f (end of file), so to speak. |
||
<br><br>Therefore, the following two REXX programs use the presence of a null line to indicate e-o-f. |
|||
<lang rexx>/*REXX program to read from the (console) default input stream until nul*/ |
|||
Therefore, the following two REXX programs use the presence of a null line to indicate e-o-f. |
|||
<syntaxhighlight lang="rexx">/*REXX program reads from the (console) default input stream until null*/ |
|||
do until _=='' |
do until _=='' |
||
parse pull _ |
parse pull _ |
||
end /*until |
end /*until*/ /*stick a fork in it, we're done.*/</syntaxhighlight> |
||
exit /*stick a fork in it, we're done.*/</lang> |
|||
===version 4=== |
===version 4=== |
||
/*REXX program |
<syntaxhighlight lang="rexx">/*REXX program reads from the (console) default input stream until null*/ |
||
do until _=='' |
do until _=='' |
||
_=linein() |
_= linein() |
||
end /*until |
end /*until*/ /*stick a fork in it, we're done.*/</syntaxhighlight> |
||
exit /*stick a fork in it, we're done.*/ |
|||
=={{header|Ring}}== |
|||
<syntaxhighlight lang="ring"> |
|||
fp = fopen("C:\Ring\ReadMe.txt","r") |
|||
r = fgetc(fp) |
|||
while isstring(r) |
|||
r = fgetc(fp) |
|||
if r = char(10) see nl |
|||
else see r ok |
|||
end |
|||
fclose(fp) |
|||
</syntaxhighlight> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
Ruby input streams are IO objects. One can use IO#each or IO#each_line to iterate lines from a stream. |
Ruby input streams are IO objects. One can use IO#each or IO#each_line to iterate lines from a stream. |
||
< |
<syntaxhighlight lang="ruby">stream = $stdin |
||
stream.each do |line| |
stream.each do |line| |
||
# process line |
# process line |
||
end</ |
end</syntaxhighlight> |
||
IO objects are also Enumerable (like Array or Range), and have methods like Enumerable#map, which call IO#each to loop through lines from a stream. |
IO objects are also Enumerable (like Array or Range), and have methods like Enumerable#map, which call IO#each to loop through lines from a stream. |
||
< |
<syntaxhighlight lang="ruby"># Create an array of lengths of every line. |
||
ary = stream.map {|line| line.chomp.length}</ |
ary = stream.map {|line| line.chomp.length}</syntaxhighlight> |
||
''To open a new stream for reading, see [[Read a file line by line#Ruby]].'' |
''To open a new stream for reading, see [[Read a file line by line#Ruby]].'' |
||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">open "\testFile.txt" for input as #f |
||
while not(eof(#f)) |
while not(eof(#f)) |
||
line input #f, a$ |
line input #f, a$ |
||
print a$ |
print a$ |
||
wend |
wend |
||
close #f</ |
close #f</syntaxhighlight> |
||
=={{header|Rust}}== |
|||
<syntaxhighlight lang="rust"> use std::io::{self, BufReader, Read, BufRead}; |
|||
use std::fs::File; |
|||
fn main() { |
|||
print_by_line(io::stdin()) |
|||
.expect("Could not read from stdin"); |
|||
File::open("/etc/fstab") |
|||
.and_then(print_by_line) |
|||
.expect("Could not read from file"); |
|||
} |
|||
fn print_by_line<T: Read>(reader: T) -> io::Result<()> { |
|||
let buffer = BufReader::new(reader); |
|||
for line in buffer.lines() { |
|||
println!("{}", line?) |
|||
} |
|||
Ok(()) |
|||
}</syntaxhighlight> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
[[Category:Scala Implementations]] |
|||
{{libheader|Scala}} |
{{libheader|Scala}} |
||
{{works with|Scala|2.10.3}} |
{{works with|Scala|2.10.3}} |
||
< |
<syntaxhighlight lang="scala"> scala.io.Source.fromFile("input.txt").getLines().foreach { |
||
line => ... }</ |
line => ... }</syntaxhighlight> |
||
=={{header|Slate}}== |
|||
<lang slate>(File newNamed: 'README') reader sessionDo: [| :input | input lines do: [| :line | inform: line]].</lang> |
|||
=={{header|sed}}== |
=={{header|sed}}== |
||
Line 1,591: | Line 2,899: | ||
5</pre> |
5</pre> |
||
The automatic printing can be suppressed with -n, |
The automatic printing can be suppressed with -n, |
||
and performed manually with p: |
|||
<pre>$ seq 5 | sed -n p |
<pre>$ seq 5 | sed -n p |
||
Line 1,601: | Line 2,910: | ||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const proc: main is func |
const proc: main is func |
||
Line 1,611: | Line 2,920: | ||
writeln("LINE: " <& line); |
writeln("LINE: " <& line); |
||
end while; |
end while; |
||
end func;</ |
end func;</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
To read from the standard input, you can use '''STDIN''' as your '''fh'''. |
To read from the standard input, you can use '''STDIN''' as your '''fh'''. |
||
< |
<syntaxhighlight lang="ruby">var file = File(__FILE__) |
||
file.open_r(\var fh, \var err) |
file.open_r(\var fh, \var err) || die "#{file}: #{err}" |
||
|| "Can't open file `#{file}': #{err}\n".die; |
|||
fh.each { |line| # iterates the lines of the fh |
fh.each { |line| # iterates the lines of the fh |
||
line.each_word { |word| # iterates the words of the line |
line.each_word { |word| # iterates the words of the line |
||
say word |
say word |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Slate}}== |
|||
<syntaxhighlight lang="slate">(File newNamed: 'README') reader sessionDo: [| :input | input lines do: [| :line | inform: line]].</syntaxhighlight> |
|||
=={{header|Smalltalk}}== |
=={{header|Smalltalk}}== |
||
< |
<syntaxhighlight lang="smalltalk">|f| |
||
f := FileStream open: 'afile.txt' mode: FileStream read. |
f := FileStream open: 'afile.txt' mode: FileStream read. |
||
[ f atEnd ] whileFalse: [ (f nextLine) displayNl ] .</ |
[ f atEnd ] whileFalse: [ (f nextLine) displayNl ] .</syntaxhighlight> |
||
=={{header|SNOBOL4}}== |
=={{header|SNOBOL4}}== |
||
< |
<syntaxhighlight lang="snobol">loop output = input :s(loop) |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Sparkling}}== |
|||
<syntaxhighlight lang="sparkling">var line; |
|||
while (line = getline()) != nil { |
|||
print(line); |
|||
}</syntaxhighlight> |
|||
=={{header|Standard ML}}== |
|||
<syntaxhighlight lang="sml">fun foldLines f init strm = |
|||
case TextIO.inputLine strm of |
|||
SOME line => foldLines f (f (line, init)) strm |
|||
| NONE => init</syntaxhighlight> |
|||
Example: Output the lines from stdin in reverse order. |
|||
<syntaxhighlight lang="sml">val () = (print o foldLines op^ "") TextIO.stdIn</syntaxhighlight> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">set fh [open $filename] |
||
while {[gets $fh line] != -1} { |
while {[gets $fh line] != -1} { |
||
# process $line |
# process $line |
||
} |
} |
||
close $fh</ |
close $fh</syntaxhighlight> |
||
For “small” files, it is often more common to do this: |
For “small” files, it is often more common to do this: |
||
< |
<syntaxhighlight lang="tcl">set fh [open $filename] |
||
set data [read $fh] |
set data [read $fh] |
||
close $fh |
close $fh |
||
foreach line [split $data \n] { |
foreach line [split $data \n] { |
||
# process line |
# process line |
||
}</ |
}</syntaxhighlight> |
||
=={{header|TUSCRIPT}}== |
=={{header|TUSCRIPT}}== |
||
< |
<syntaxhighlight lang="tuscript"> |
||
$$ MODE TUSCRIPT |
$$ MODE TUSCRIPT |
||
file="a.txt" |
file="a.txt" |
||
Line 1,659: | Line 2,984: | ||
ENDLOOP |
ENDLOOP |
||
ENDACCESS source |
ENDACCESS source |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header| |
=={{header|TypeScript}}== |
||
<syntaxhighlight lang="typescript"> |
|||
the pipe 'yes XXX' produces a sequence |
|||
#!/usr/bin/env node |
|||
import EventEmitter from 'events'; |
|||
read by lines |
|||
<lang bash>yes 'A B C D ' | while read x ; do echo -$x- ; done</lang> |
|||
function stdinLineByLine() { |
|||
read by words |
|||
const stdin = new EventEmitter(); |
|||
<lang bash>yes 'A B C D ' | while read -d\ a ; do echo -$a- ; done</lang> |
|||
let buff = ''; |
|||
let lines; |
|||
process.stdin |
|||
.on('data', (data) => { |
|||
buff += data; |
|||
lines = buff.split(/\r\n|\n/); |
|||
buff = lines.pop(); |
|||
lines.forEach((line) => stdin.emit('line', line)); |
|||
}) |
|||
.on('end', () => { |
|||
if (buff.length > 0) stdin.emit('line', buff); |
|||
}); |
|||
return stdin; |
|||
} |
|||
const stdin = stdinLineByLine(); |
|||
stdin.on('line', console.log); |
|||
</syntaxhighlight> |
|||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
When there is something to do with the input, here is a loop: |
When there is something to do with the input, here is a loop: |
||
< |
<syntaxhighlight lang="bash">while read line ; do |
||
# examine or do something to the text in the "line" variable |
# examine or do something to the text in the "line" variable |
||
echo "$line" |
echo "$line" |
||
done</ |
done</syntaxhighlight> |
||
The following echoes standard input to standard output line-by-line until the end of the stream. |
The following echoes standard input to standard output line-by-line until the end of the stream. |
||
< |
<syntaxhighlight lang="bash">cat < /dev/stdin > /dev/stdout</syntaxhighlight> |
||
Since <code>cat</code> defaults to reading from standard input and writing to standard output, this can be further simplified to the following. |
Since <code>cat</code> defaults to reading from standard input and writing to standard output, this can be further simplified to the following. |
||
<lang |
<syntaxhighlight lang="bash">cat</syntaxhighlight> |
||
=={{header|UnixPipes}}== |
|||
the pipe 'yes XXX' produces a sequence |
|||
read by lines: |
|||
<syntaxhighlight lang="bash">yes 'A B C D ' | while read x ; do echo -$x- ; done</syntaxhighlight> |
|||
read by words: |
|||
<syntaxhighlight lang="bash">yes 'A B C D ' | while read -d\ a ; do echo -$a- ; done</syntaxhighlight> |
|||
=={{header|Ursa}}== |
|||
<syntaxhighlight lang="ursa">decl file f |
|||
f.open "filename.txt" |
|||
while (f.hasnext) |
|||
out (in string f) endl console |
|||
end while</syntaxhighlight> |
|||
=={{header|Vala}}== |
=={{header|Vala}}== |
||
< |
<syntaxhighlight lang="vala">int main() { |
||
string? s; |
string? s; |
||
while((s = stdin.read_line()) != null) { |
while((s = stdin.read_line()) != null) { |
||
Line 1,687: | Line 3,049: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
=={{header|VBA}}== |
|||
<syntaxhighlight lang="vb">Public Sub test() |
|||
Dim filesystem As Object, stream As Object, line As String |
|||
Set filesystem = CreateObject("Scripting.FileSystemObject") |
|||
Set stream = filesystem.OpenTextFile("D:\test.txt") |
|||
Do While stream.AtEndOfStream <> True |
|||
line = stream.ReadLine |
|||
Debug.Print line |
|||
Loop |
|||
stream.Close |
|||
End Sub</syntaxhighlight> |
|||
=={{header|VBScript}}== |
|||
<syntaxhighlight lang="vb"> |
|||
filepath = "SPECIFY PATH TO TEXT FILE HERE" |
|||
Set objFSO = CreateObject("Scripting.FileSystemObject") |
|||
Set objInFile = objFSO.OpenTextFile(filepath,1,False,0) |
|||
Do Until objInFile.AtEndOfStream |
|||
line = objInFile.ReadLine |
|||
WScript.StdOut.WriteLine line |
|||
Loop |
|||
objInFile.Close |
|||
Set objFSO = Nothing |
|||
</syntaxhighlight> |
|||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
Line 1,693: | Line 3,083: | ||
This reads a stream line by line, outputing each line to the screen. |
This reads a stream line by line, outputing each line to the screen. |
||
< |
<syntaxhighlight lang="vbnet">Sub Consume(ByVal stream As IO.StreamReader) |
||
Dim line = stream.ReadLine |
Dim line = stream.ReadLine |
||
Do Until line Is Nothing |
Do Until line Is Nothing |
||
Line 1,699: | Line 3,089: | ||
line = stream.ReadLine |
line = stream.ReadLine |
||
Loop |
Loop |
||
End Sub</ |
End Sub</syntaxhighlight> |
||
=={{header| |
=={{header|V (Vlang)}}== |
||
<syntaxhighlight lang="Zig"> |
|||
Many objects support "stream of" concepts such as lines, characters, chunks. Some are File, Data (bit bucket), List, Console. Word by word isn't explicitly supported. If an object is stream-able, it supports methods like foreach, pump, apply, reduce, etc. |
|||
import os |
|||
<lang zkl>foreach line in (File("foo.txt")){...} |
|||
List(1,2,3).readln() // here, a "line" is a list element |
|||
fn main() { |
|||
Utils.Helpers.zipWith(False, // enumerate a file |
|||
mut ay_view_content := []string{} |
|||
fcn(n,line){"%3d: %s".fmt(n,line).print()},[1..],File("cmp.zkl"))</lang> |
|||
file := "./input.txt" |
|||
// check if file exists |
|||
if os.is_file(file) == false { |
|||
print("Error: '${file}' not found") |
|||
exit(-1) |
|||
} |
|||
ay_view_content << os.read_lines(file) or {print(err) exit(-2)} |
|||
for line in ay_view_content { |
|||
if line !="" {println(line)} |
|||
if line =="" {println("Found blank line!")} |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|Wren}}== |
|||
<syntaxhighlight lang="wren">import "io" for File |
|||
File.open("input.txt") { |file| |
|||
{{omit from|PARI/GP|No access to streams other than input}} |
|||
var offset = 0 |
|||
{{omit from|TI-89 BASIC}} <!-- No streams other than user input, not really applicable. --> |
|||
var line = "" |
|||
while(true) { |
|||
var b = file.readBytes(1, offset) |
|||
offset = offset + 1 |
|||
if (b == "\n") { |
|||
// process 'line' |
|||
line = "" // reset line variable |
|||
} else if (b == "\r") { // Windows |
|||
// wait for following "\n" |
|||
} else if (b == "") { // end of stream |
|||
return |
|||
} else { |
|||
line = line + b |
|||
} |
|||
} |
|||
}</syntaxhighlight> |
|||
=={{header|x86 Assembly}}== |
|||
'''GAS, 64 bit (Linux)''': Compiled with <code>gcc -nostdlib</code>. Memory maps the file and outputs one line at a time. Try <code>./a.out file</code>, <code>./a.out < file</code>, or <code>./a.out <<< "Heredoc"</code>. It's a little like cat, but less functional. |
|||
[[Category:Streams]] |
|||
<syntaxhighlight lang="x86">#define SYS_WRITE $1 |
|||
#define SYS_OPEN $2 |
|||
#define SYS_CLOSE $3 |
|||
#define SYS_FSTAT $5 |
|||
#define SYS_MMAP $9 |
|||
#define SYS_MUNMAP $11 |
|||
#define SYS_EXIT $60 |
|||
// From experiments: |
|||
#define FSIZEOFF 48 |
|||
#define STATSIZE 144 |
|||
// From Linux source: |
|||
#define RDONLY $00 |
|||
#define PROT_READ $0x1 |
|||
#define MAP_PRIVATE $0x02 |
|||
#define STDIN $0 |
|||
#define STDOUT $1 |
|||
.global _start |
|||
.text |
|||
/* Details: */ |
|||
/* |
|||
Remember: %rax(%rdi, %rsi, %rdx, %r10, %r8, %r9) |
|||
- Open a file (get its fd) |
|||
- int fd = open("filename", RDONLY) |
|||
- Get its filesize: |
|||
- fstat(fd, statstruct). 0 if ok. fsize at statstruct+48 |
|||
- Then memory map it. |
|||
- void* vmemptr = mmap(vmemptr, fsize, PROT_READ, MAP_PRIVATE, fd, 0) |
|||
- Scan for newlines, print line. |
|||
- Keep going until done. Details at 11. |
|||
- Unmap memory |
|||
- munmap(vmemptr, filesize). 0 if ok. |
|||
- Exit |
|||
*/ |
|||
.macro ERRCHECK code |
|||
cmpq $\code, %rax |
|||
je fs_error |
|||
.endm |
|||
/* Local stack notes: |
|||
0: int fd |
|||
4: void* vmemptr |
|||
12: void* head |
|||
20: void* lookahead |
|||
28: void* end |
|||
*/ |
|||
_start: |
|||
// Open: |
|||
movq RDONLY, %rsi |
|||
// Filename ptr is on stack currently as argv[1]: |
|||
cmpq $1, (%rsp) // if argc is 1, default to stdin |
|||
jnz open_file |
|||
subq $36, %rsp // local stack |
|||
movl STDIN, (%rsp) |
|||
jmp fstat |
|||
open_file: |
|||
movq 16(%rsp), %rdi // argc(8), argv0(8) => rsp+16. filename |
|||
movq SYS_OPEN, %rax |
|||
syscall |
|||
ERRCHECK -1 |
|||
subq $36, %rsp // local stack |
|||
movl %eax, (%rsp) // int fd = open(argv[1], RDONLY) |
|||
// fstat to get filesize |
|||
fstat: |
|||
movq $statstruct, %rsi |
|||
movl (%rsp), %edi // fd |
|||
movq SYS_FSTAT, %rax |
|||
syscall // fstat(fd, statstruct) |
|||
ERRCHECK -1 |
|||
// mmap - don't forget to munmap. |
|||
mmap: |
|||
movq $0, %r9 // offset |
|||
movq (%rsp), %r8 // fd |
|||
movq MAP_PRIVATE, %r10 |
|||
movq PROT_READ, %rdx |
|||
movq filesize, %rsi |
|||
movq (%rsp), %rdi // vmemptr |
|||
movq SYS_MMAP, %rax |
|||
syscall |
|||
ERRCHECK -1 |
|||
movq %rax, 4(%rsp) // void* vmemptr = mmap(vmemptr, fsize, PROT_READ, MAP_PRIVATE, fd, 0) |
|||
/* Print lines */ |
|||
movq %rax, 12(%rsp) // head = vmemptr |
|||
addq filesize, %rax |
|||
decq %rax |
|||
movq %rax, 28(%rsp) // end = vmemptr+filesize-1 |
|||
scan_outer: |
|||
movq 12(%rsp), %rax |
|||
cmpq 28(%rsp), %rax |
|||
jge cleanup // if head >= end, done |
|||
movq %rax, %rbx // Using rbx as lookahead |
|||
scan_inner: |
|||
cmpq 28(%rsp), %rbx |
|||
jge writeline // if lookahead >= end, write the line. |
|||
cmpb $'\n, (%rbx) |
|||
jz writeline // if '\n'==*lookahead, write the line |
|||
incq %rbx |
|||
jmp scan_inner |
|||
writeline: |
|||
// write: |
|||
incq %rbx |
|||
movq %rbx, %rdx |
|||
subq 12(%rsp), %rdx // rdx <- lookahead-head |
|||
movq 12(%rsp), %rsi |
|||
movq STDOUT, %rdi |
|||
movq SYS_WRITE, %rax |
|||
syscall // write(stdout, head, lookahead-head) |
|||
safety: |
|||
movq %rbx, 12(%rsp) // head = lookahead. |
|||
jmp scan_outer |
|||
cleanup: |
|||
// munmap |
|||
movq filesize, %rsi |
|||
movq 4(%rsp), %rdi |
|||
movq SYS_MUNMAP, %rax |
|||
syscall // munmap(vmemptr, filesize) |
|||
cmpq $-1, %rax |
|||
je fs_error |
|||
// close |
|||
movl (%rsp), %edi |
|||
movq SYS_CLOSE, %rax |
|||
syscall // close(fd) |
|||
ERRCHECK -1 |
|||
exit: |
|||
movq SYS_EXIT, %rax |
|||
xorq %rdi, %rdi // The exit code. |
|||
syscall |
|||
fs_error: |
|||
movq SYS_EXIT, %rax |
|||
movq $-1, %rdi |
|||
syscall // exit(-1) |
|||
.data |
|||
statstruct: // This struct is 144 bytes. Only want size (+48) |
|||
.zero FSIZEOFF |
|||
filesize: // 8 bytes. |
|||
.quad 0 |
|||
.zero STATSIZE-FSIZEOFF+8</syntaxhighlight> |
|||
=={{header|XPL0}}== |
|||
Text stream is a file redirected on command line i.e: <file.txt |
|||
<syntaxhighlight lang="xpl0">int C; |
|||
[repeat \read file |
|||
repeat \read line |
|||
repeat C:= ChIn(1); \read word |
|||
ChOut(0, C); |
|||
until (C<^A ! C>^z) & (C<^0 ! C>^9); \non-alphanumeric |
|||
until C < $20; \CR, LF, or EOF |
|||
until C = \EOF\ $1A; |
|||
]</syntaxhighlight> |
|||
=={{header|zkl}}== |
|||
Many objects support "stream of" concepts such as lines, characters, chunks. |
|||
Some are File, Data (bit bucket), List, Console. |
|||
Word by word isn't explicitly supported. |
|||
If an object is stream-able, it supports methods like foreach, pump, apply, reduce, etc. |
|||
<syntaxhighlight lang="zkl">foreach line in (File("foo.txt")){...} |
|||
List(1,2,3).readln() // here, a "line" is a list element |
|||
Utils.Helpers.zipWith(False, // enumerate a file |
|||
fcn(n,line){"%3d: %s".fmt(n,line).print()},[1..],File("cmp.zkl"))</syntaxhighlight> |
Latest revision as of 17:01, 11 July 2024
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
- Task
Read from a text stream either word-by-word or line-by-line until the stream runs out of data.
The stream will have an unknown amount of data on it.
AArch64 Assembly
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program inputLoop64.s */
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly*/
.include "../includeConstantesARM64.inc"
.equ BUFSIZE, 10000
.equ LINESIZE, 100
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessErreur: .asciz "Erreur ouverture fichier input.\n"
szMessErreur1: .asciz "Erreur fermeture fichier.\n"
szMessErreur2: .asciz "Erreur lecture fichier.\n"
szCarriageReturn: .asciz "\n"
szMessEndLine: .asciz "<<<<<< End line.\n"
szMessCodeErr: .asciz "Error code décimal : @ \n"
szNameFileInput: .asciz "input.txt"
/*******************************************/
/* DONNEES NON INITIALISEES */
/*******************************************/
.bss
sZoneConv: .skip 24
sBuffer: .skip BUFSIZE
sBufferWord: .skip LINESIZE
/**********************************************/
/* -- Code section */
/**********************************************/
.text
.global main
main:
/* open file */
mov x0,AT_FDCWD
ldr x1,qAdrszNameFileInput // file name
mov x2,O_RDONLY // flags
mov x3,0 // mode
mov x8,OPEN // call system OPEN
svc 0
cmp x0,0 // open error ?
ble erreur
/* read file */
mov x9,x0 // save File Descriptor
ldr x1,qAdrsBuffer // buffer address
mov x2,BUFSIZE // buffer size
mov x8,READ // call system READ
svc 0
cmp x0,0 // read error ?
ble erreur2
mov x2,x0 // length read characters
/* buffer analyze */
ldr x3,qAdrsBuffer // buffer address
ldr x5,qAdrsBufferWord // buffer address
mov x7,0 // word byte counter
mov x4,0 // byte counter
mov x10,1
1:
ldrb w6,[x3,x4] // load byte buffer
cmp x6,' ' // space ?
csel x8,xzr,x10,eq // yes 0-> x8
beq 2f
cmp x6,0xA // end line ?
csel x8,x10,xzr,eq // yes 1 -> x8
beq 2f
cmp x6,0xD // end line ?
csel x8,x10,xzr,eq
beq 3f
strb w6,[x5,x7] // store byte
add x7,x7,1 // increment word byte counter
b 4f
2: // word end
cmp x7,0
beq 3f
mov x6,0 // store 0 final
strb w6,[x5,x7]
mov x0,x5 // display word
bl affichageMess
ldr x0,qAdrszCarriageReturn
bl affichageMess
mov x7,0 // raz word byte counter
3:
cmp x8,1 // end line ?
bne 4f
ldr x0,qAdrszMessEndLine
bl affichageMess
4:
add x4,x4,1 // increment read buffer counter
cmp x4,x2 // end bytes ?
blt 1b // no -> loop
4:
/* close imput file */
mov x0,x9 // Fd
mov x8,CLOSE // call system CLOSE
svc 0
cmp x0,0 // close error ?
blt erreur1
mov x0,0 // return code OK
b 100f
erreur:
ldr x1,qAdrszMessErreur
bl displayError
mov x0,1 // error return code
b 100f
erreur1:
ldr x1,qAdrszMessErreur1
bl displayError
mov x0,1 // error return code
b 100f
erreur2:
ldr x1,qAdrszMessErreur2
bl displayError
mov x0,1 // error return code
b 100f
100: // end program
mov x8,EXIT
svc 0
qAdrszNameFileInput: .quad szNameFileInput
qAdrszMessErreur: .quad szMessErreur
qAdrszMessErreur1: .quad szMessErreur1
qAdrszMessErreur2: .quad szMessErreur2
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrszMessEndLine: .quad szMessEndLine
qAdrsBuffer: .quad sBuffer
qAdrsBufferWord: .quad sBufferWord
/******************************************************************/
/* display error message */
/******************************************************************/
/* x0 contains error code */
/* x1 contains address error message */
displayError:
stp x2,lr,[sp,-16]! // save registers
mov x2,x0 // save error code
mov x0,x1 // display message error
bl affichageMess
mov x0,x2
ldr x1,qAdrsZoneConv // conversion error code
bl conversion10S // decimal conversion
ldr x0,qAdrszMessCodeErr
ldr x1,qAdrsZoneConv
bl strInsertAtCharInc // insert result at @ character
bl affichageMess // display message final
ldp x2,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
qAdrsZoneConv: .quad sZoneConv
qAdrszMessCodeErr: .quad szMessCodeErr
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
Action!
PROC ReadStream(BYTE stream)
CHAR ARRAY line(255)
WHILE Eof(stream)=0
DO
InputSD(stream,line)
PrintE(line)
OD
RETURN
PROC Main()
BYTE streamId=[1]
Close(streamId)
Open(streamId,"H6:INPUT_PU.ACT",4)
PrintE("Reading from stream...") PutE()
ReadStream(streamId)
Close(streamId)
RETURN
- Output:
Screenshot from Atari 8-bit computer
Reading from stream... PROC ReadStream(BYTE stream) CHAR ARRAY line(255) WHILE Eof(stream)=0 DO InputSD(stream,line) PrintE(line) OD RETURN PROC Main() BYTE streamId=[1] Close(streamId) Open(streamId,"H6:INPUT_PU.ACT",4) PrintE("Reading from stream...") PutE() ReadStream(streamId) Close(streamId) RETURN
Ada
This example reads in a text stream from standard input line by line and writes the output to standard output.
with Ada.Text_Io; use Ada.Text_Io;
procedure Read_Stream is
Line : String(1..10);
Length : Natural;
begin
while not End_Of_File loop
Get_Line(Line, Length); -- read up to 10 characters at a time
Put(Line(1..Length));
-- The current line of input data may be longer than the string receiving the data.
-- If so, the current input file column number will be greater than 0
-- and the extra data will be unread until the next iteration.
-- If not, we have read past an end of line marker and col will be 1
if Col(Current_Input) = 1 then
New_Line;
end if;
end loop;
end Read_Stream;
Aime
void
read_stream(file f)
{
text s;
while (f_line(f, s) != -1) {
# the read line available as -s-
}
}
ALGOL 68
For file consisting of just one page - a typical linux/unix file:
main:(
PROC raise logical file end = (REF FILE f) BOOL: ( except logical file end );
on logical file end(stand in, raise logical file end);
DO
print(read string);
read(new line);
print(new line)
OD;
except logical file end:
SKIP
)
For multi page files, each page is seekable with PROC set = (REF FILE file, INT page, line, char)VOID: ~. This allows rudimentary random access where each new page is effectively a new record.
main:(
PROC raise logical file end = (REF FILE f) BOOL: ( except logical file end );
on logical file end(stand in, raise logical file end);
DO
PROC raise page end = (REF FILE f) BOOL: ( except page end );
on page end(stand in, raise page end);
DO
print(read string);
read(new line);
print(new line)
OD;
except page end:
read(new page);
print(new page)
OD;
except logical file end:
SKIP
)
The boolean functions physical file ended(f), logical file ended(f), page ended(f) and line ended(f) are also available to indicate the end of a file, page and line.
ALGOL W
begin
string(80) line;
% allow the program to continue after reaching end-of-file %
% without this, end-of-file would cause a run-time error %
ENDFILE := EXCEPTION( false, 1, 0, false, "EOF" );
% read lines until end of file %
read( line );
while not XCPNOTED(ENDFILE) do begin
write( line );
read( line )
end
end.
AmigaE
CONST BUFLEN=1024, EOF=-1
PROC consume_input(fh)
DEF buf[BUFLEN] : STRING, r
REPEAT
/* even if the line si longer than BUFLEN,
ReadStr won't overflow; rather the line is
"splitted" and the remaining part is read in
the next ReadStr */
r := ReadStr(fh, buf)
IF buf[] OR (r <> EOF)
-> do something
WriteF('\s\n',buf)
ENDIF
UNTIL r=EOF
ENDPROC
PROC main()
DEF fh
fh := Open('basicinputloop.e', OLDFILE)
IF fh
consume_input(fh)
Close(fh)
ENDIF
ENDPROC
APL
h ← ⊃ (⎕fio['read_text'] 'corpus/sample1.txt')
⍴h
7 49
]boxing 8
h
┌→────────────────────────────────────────────────┐
↓This is some sample text. │
│The text itself has multiple lines, and │
│the text has some words that occur multiple times│
│in the text. │
│ │
│This is the end of the text. │
│ │
└─────────────────────────────────────────────────┘
ARM Assembly
/* ARM assembly Raspberry PI */
/* program inputLoop.s */
/* REMARK 1 : this program use routines in a include file
see task Include a file language arm assembly
for the routine affichageMess conversion10
see at end of this program the instruction include */
/*********************************************/
/*constantes */
/********************************************/
.equ STDOUT, 1 @ Linux output console
.equ EXIT, 1 @ Linux syscall
.equ READ, 3
.equ WRITE, 4
.equ OPEN, 5
.equ CLOSE, 6
.equ CREATE, 8
/* file */
.equ O_RDONLY, 0x0 @ open for reading only
.equ BUFSIZE, 10000
.equ LINESIZE, 100
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessErreur: .asciz "Erreur ouverture fichier input.\n"
szMessErreur1: .asciz "Erreur fermeture fichier.\n"
szMessErreur2: .asciz "Erreur lecture fichier.\n"
szCarriageReturn: .asciz "\n"
szMessEndLine: .asciz "<<<<<< End line.\n"
szNameFileInput: .asciz "input.txt"
/*******************************************/
/* DONNEES NON INITIALISEES */
/*******************************************/
.bss
sBuffer: .skip BUFSIZE
sBufferWord: .skip LINESIZE
/**********************************************/
/* -- Code section */
/**********************************************/
.text
.global main
main:
/* open file */
ldr r0,iAdrszNameFileInput @ file name
mov r1,#O_RDONLY @ flags
mov r2,#0 @ mode
mov r7,#OPEN @ call system OPEN
svc #0
cmp r0,#0 @ open error ?
ble erreur
/* read file */
mov r9,r0 @ save File Descriptor
ldr r1,iAdrsBuffer @ buffer address
mov r2,#BUFSIZE @ buffer size
mov r7, #READ @ call system READ
svc 0
cmp r0,#0 @ read error ?
ble erreur2
mov r2,r0 @ length read characters
/* buffer analyze */
ldr r3,iAdrsBuffer @ buffer address
ldr r5,iAdrsBufferWord @ buffer address
mov r7,#0 @ word byte counter
mov r4,#0 @ byte counter
1:
ldrb r6,[r3,r4] @ load byte buffer
cmp r6,#' ' @ space ?
moveq r8,#0 @ yes
beq 2f
cmp r6,#0xA @ end line ?
moveq r8,#1
beq 2f
cmp r6,#0xD @ end line ?
beq 3f
strb r6,[r5,r7] @ store byte
add r7,#1 @ increment word byte counter
b 4f
2: @ word end
cmp r7,#0
beq 3f
mov r6,#0 @ store 0 final
strb r6,[r5,r7]
mov r0,r5 @ display word
bl affichageMess
ldr r0,iAdrszCarriageReturn
bl affichageMess
mov r7,#0 @ raz word byte counter
3:
cmp r8,#1 @ end line ?
bne 4f
ldr r0,iAdrszMessEndLine
bl affichageMess
4:
add r4,#1 @ increment read buffer counter
cmp r4,r2 @ end bytes ?
blt 1b @ no -> loop
4:
/* close imput file */
mov r0,r9 @ Fd
mov r7, #CLOSE @ call system CLOSE
svc 0
cmp r0,#0 @ close error ?
blt erreur1
mov r0,#0 @ return code OK
b 100f
erreur:
ldr r1,iAdrszMessErreur
bl displayError
mov r0,#1 @ error return code
b 100f
erreur1:
ldr r1,iAdrszMessErreur1
bl displayError
mov r0,#1 @ error return code
b 100f
erreur2:
ldr r1,iAdrszMessErreur2
bl displayError
mov r0,#1 @ error return code
b 100f
100: @ end program
mov r7, #EXIT
svc 0
iAdrszNameFileInput: .int szNameFileInput
iAdrszMessErreur: .int szMessErreur
iAdrszMessErreur1: .int szMessErreur1
iAdrszMessErreur2: .int szMessErreur2
iAdrszCarriageReturn: .int szCarriageReturn
iAdrszMessEndLine: .int szMessEndLine
iAdrsBuffer: .int sBuffer
iAdrsBufferWord: .int sBufferWord
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
.include "../affichage.inc"
- Output:
word1 word2 word3 <<<<<< End line. Line2w1 line2w2 <<<<<< End line. Oneword <<<<<< End line. <<<<<< End line. AfterEmptyLine <<<<<< End line.
Arturo
while [true][
i: input "> "
print i
]
AutoHotkey
This example reads the text of a source file line by line and writes the output to a destination file.
Loop, Read, Input.txt, Output.txt
{
FileAppend, %A_LoopReadLine%`n
}
AWK
This just reads lines from stdin and prints them until EOF is read.
{ print $0 }
or, more idiomatic:
1
BASIC
BASIC256
f = freefile
open f, "test.txt"
while not eof(f)
linea$ = readline(f)
print linea$ # echo to the console
end while
close f
end
OxygenBasic
uses Console
uses ParseUtil
string s=getfile "t.txt"
int le=len s
int i=1
while i<le
print GetNextLine(s,i) cr
wend
pause
QBasic
OPEN "test.txt" FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, linea$
PRINT linea$ ' echo to the console
WEND
CLOSE #1
END
True BASIC
OPEN #1: NAME "test.txt", ACCESS INPUT
DO
LINE INPUT #1: linea$
PRINT linea$ ! echo to the console
LOOP UNTIL END #1
CLOSE #1
END
Yabasic
filename$ = "test.txt"
f = open(filename$)
if not f error "Could not open '" + filename$ + "' for reading"
while(not eof(f))
line input #f linea$
print linea$ // echo to the console
wend
close f
end
Applesoft BASIC
100 INPUT "FILENAME:";F$
110 D$ = CHR$(4)
120 PRINT D$"VERIFY"F$
130 PRINT D$"OPEN"F$
140 PRINT D$"READ"F$
150 ONERR GOTO 190
160 GET C$
170 PRINT CHR$(0)C$;
180 GOTO 160
190 POKE 216,0
200 IF PEEK(222) <> 5 THEN RESUME
210 PRINT D$"CLOSE"F$
BaCon
'--- some generic header file to give it a real test
PRINT "Enter any file name you want to read ex: /usr/include/X11/X.h"
INPUT filename$
text$ = LOAD$(filename$)
SPLIT text$ BY NL$ TO TOK$ SIZE dim
i = 0
'---dynamic index the end of an array is always null terminated
WHILE (TOK$[i] ISNOT NULL)
PRINT TOK$[i]
INCR i
WEND
BBC BASIC
This specifically relates to console input (stdin).
STD_INPUT_HANDLE = -10
STD_OUTPUT_HANDLE = -11
SYS "GetStdHandle", STD_INPUT_HANDLE TO @hfile%(1)
SYS "GetStdHandle", STD_OUTPUT_HANDLE TO @hfile%(2)
SYS "SetConsoleMode", @hfile%(1), 0
*INPUT 13
*OUTPUT 14
REPEAT
INPUT A$
PRINT A$
UNTIL FALSE
Commodore BASIC
This example should generally work across all Commodore models. It is important to note that when using the reserved variable STATUS (or ST for short) to detect the end of file marker (bit 6), the KERNAL updates this variable based on any previous I/O operation, including the OPEN command, but makes no indication as to which I/O access the status applies to. Therefore, if using the disk drive, it is best to open the command file number and channel (15) before opening the actual file on disk, and leave the command channel open while performing the operations to open the file and access it as shown in the example.
10 rem input loop - rosetta code
11 rem open command channel, clear screen, switch to lower case
12 open 15,8,15
15 print chr$(147);chr$(14):f$=""
20 input "Enter filename";f$
25 if f$="" then end
30 open 5,8,5,f$+",s,r"
40 gosub 1000
50 if er=62 then print "That file is not found... Try again.":close 5:goto 20
60 if er<>0 then print "There was an unexpected error.":close 5:gosub 1100
70 get#5,a$
80 if st and 64 then close 5:close 15:end
90 print a$;:goto 70
1000 rem check command channel for error
1005 rem error number, error msg$, track number, sector number
1010 input#15,er,er$,tk,sc
1020 return
1100 rem print error
1110 print:print er;"- ";er$;" track:";tk;"sector:";sc
1120 return
IS-BASIC
100 PROGRAM "Type.bas"
110 TEXT 80
120 INPUT PROMPT "File name: ":F$
130 WHEN EXCEPTION USE IOERROR
140 OPEN #1:F$ ACCESS INPUT
150 DO
160 LINE INPUT #1,IF MISSING EXIT DO:F$
170 PRINT F$
180 LOOP
190 CLOSE #1
200 END WHEN
210 HANDLER IOERROR
220 PRINT EXSTRING$(EXTYPE)
230 END
240 END HANDLER
Alternate solution:
100 PROGRAM "Type.bas"
110 INPUT PROMPT "File name: ":F$
120 WHEN EXCEPTION USE IOERROR
130 OPEN #1:F$
140 COPY FROM #1 TO #0
150 CLOSE #1
160 END WHEN
170 HANDLER IOERROR
180 PRINT EXSTRING$(EXTYPE)
190 CLOSE #1
200 END HANDLER
uBasic/4tH
If Set (a, Open ("myfile.bas", "r")) < 0 Then Print "Cannot open \qmyfile.bas\q" : End
Do While Read (a)
Print Show(Tok(0))
Loop
Close a
This version tokenizes the line read and prints all individual words.
If Set (a, Open ("myfile.bas", "r")) < 0 Then Print "Cannot open \qmyfile.bas\q" : End
Do While Read (a) ' get next line
Do
p = Here() ' save cursor position
s = Tok(Ord(" ")) ' tokenize next word
Until p = Here() ' quit if cursor didn't progress
If Len(s) Then Print Show(s) ' print only full words
Loop ' next word
Loop ' next line
Close a
SmallBASIC
Open a text file and print content line by line:
open "input.txt" for input as #1
while(!eof(1))
input #1, c
print c
wend
close #1
Batch File
for /f %%i in (file.txt) do if %%i@ neq @ echo %%i
Bracmat
This example first creates a test file with three lines. It then opens the file in read mode, sets the string of break characters and then reads the file token by token, where tokens are delimeted by break characters. Finally, the file position is set to an invalid value, which closes the file.
( put$("This is
a three line
text","test.txt",NEW)
& fil$("test.txt",r)
& fil$(,STR," \t\r\n")
& 0:?linenr
& whl
' ( fil$:(?line.?breakchar)
& put
$ ( str
$ ( "breakchar:"
( !breakchar:" "&SP
| !breakchar:\t&"\\t"
| !breakchar:\r&"\\r"
| !breakchar:\n&"\\n"
| !breakchar:&EOF
)
", word "
(1+!linenr:?linenr)
":"
!line
\n
)
)
)
& (fil$(,SET,-1)|out$"file closed")
);
- Output:
breakchar:SP, word 1:This breakchar:\n, word 2:is breakchar:SP, word 3:a breakchar:SP, word 4:three breakchar:\n, word 5:line breakchar:EOF, word 6:text file closed
C
Reads arbitrarily long line each time and return a null-terminated string. Caller is responsible for freeing the string.
#include <stdlib.h>
#include <stdio.h>
char *get_line(FILE* fp)
{
int len = 0, got = 0, c;
char *buf = 0;
while ((c = fgetc(fp)) != EOF) {
if (got + 1 >= len) {
len *= 2;
if (len < 4) len = 4;
buf = realloc(buf, len);
}
buf[got++] = c;
if (c == '\n') break;
}
if (c == EOF && !got) return 0;
buf[got++] = '\0';
return buf;
}
int main()
{
char *s;
while ((s = get_line(stdin))) {
printf("%s",s);
free(s);
}
return 0;
}
C#
using System;
using System.IO;
class Program
{
static void Main(string[] args)
{
// For stdin, you could use
// new StreamReader(Console.OpenStandardInput(), Console.InputEncoding)
using (var b = new StreamReader("file.txt"))
{
string line;
while ((line = b.ReadLine()) != null)
Console.WriteLine(line);
}
}
}
C++
The following functions send the words resp. lines to a generic output iterator.
#include <istream>
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>
// word by word
template<class OutIt>
void read_words(std::istream& is, OutIt dest)
{
std::string word;
while (is >> word)
{
// send the word to the output iterator
*dest = word;
}
}
// line by line:
template<class OutIt>
void read_lines(std::istream& is, OutIt dest)
{
std::string line;
while (std::getline(is, line))
{
// store the line to the output iterator
*dest = line;
}
}
int main()
{
// 1) sending words from std. in std. out (end with Return)
read_words(std::cin,
std::ostream_iterator<std::string>(std::cout, " "));
// 2) appending lines from std. to vector (end with Ctrl+Z)
std::vector<std::string> v;
read_lines(std::cin, std::back_inserter(v));
return 0;
}
An alternate way to read words or lines is to use istream iterators:
template<class OutIt>
void read_words(std::istream& is, OutIt dest)
{
typedef std::istream_iterator<std::string> InIt;
std::copy(InIt(is), InIt(),
dest);
}
namespace detail
{
struct ReadableLine : public std::string
{
friend std::istream & operator>>(std::istream & is, ReadableLine & line)
{
return std::getline(is, line);
}
};
}
template<class OutIt>
void read_lines(std::istream& is, OutIt dest)
{
typedef std::istream_iterator<detail::ReadableLine> InIt;
std::copy(InIt(is), InIt(),
dest);
}
Clojure
(defn basic-input [fname]
(line-seq (java.io.BufferedReader. (java.io.FileReader. fname))))
COBOL
IDENTIFICATION DIVISION.
PROGRAM-ID. input-loop.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT in-stream ASSIGN TO KEYBOARD *> or any other file/stream
ORGANIZATION LINE SEQUENTIAL
FILE STATUS in-stream-status.
DATA DIVISION.
FILE SECTION.
FD in-stream.
01 stream-line PIC X(80).
WORKING-STORAGE SECTION.
01 in-stream-status PIC 99.
88 end-of-stream VALUE 10.
PROCEDURE DIVISION.
OPEN INPUT in-stream
PERFORM UNTIL EXIT
READ in-stream
AT END
EXIT PERFORM
END-READ
DISPLAY stream-line
END-PERFORM
CLOSE in-stream
.
END PROGRAM input-loop.
Common Lisp
(defun basic-input (filename)
(with-open-file (stream (make-pathname :name filename) :direction :input)
(loop for line = (read-line stream nil nil)
while line
do (format t "~a~%" line))))
D
void main() {
import std.stdio;
immutable fileName = "input_loop1.d";
foreach (const line; fileName.File.byLine) {
pragma(msg, typeof(line)); // Prints: const(char[])
// line is a transient slice, so if you need to
// retain it for later use, you have to .dup or .idup it.
line.writeln; // Do something with each line.
}
// Keeping the line terminators:
foreach (const line; fileName.File.byLine(KeepTerminator.yes)) {
// line is a transient slice.
line.writeln;
}
foreach (const string line; fileName.File.lines) {
// line is a transient slice.
line.writeln;
}
}
import tango.io.Console;
import tango.text.stream.LineIterator;
void main (char[][] args) {
foreach (line; new LineIterator!(char)(Cin.input)) {
// do something with each line
}
}
import tango.io.Console;
import tango.text.stream.SimpleIterator;
void main (char[][] args) {
foreach (word; new SimpleIterator!(char)(" ", Cin.input)) {
// do something with each word
}
}
Note that foreach variables 'line' and 'word' are transient slices. If you need to retain them for later use, you should .dup them.
Delphi
program InputLoop;
{$APPTYPE CONSOLE}
uses SysUtils, Classes;
var
lReader: TStreamReader; // Introduced in Delphi XE
begin
lReader := TStreamReader.Create('input.txt', TEncoding.Default);
try
while lReader.Peek >= 0 do
Writeln(lReader.ReadLine);
finally
lReader.Free;
end;
end.
Déjà Vu
while /= :eof dup !read-line!stdin:
!print( "Read a line: " !decode!utf-8 swap )
drop
!print "End of file."
EasyLang
repeat
l$ = input
until error = 1
print l$
.
Eiffel
note
description : "{
There are several examples included, including input from a text file,
simple console input and input from standard input explicitly.
See notes in the code for details.
Examples were compile using Eiffel Studio 6.6 with only the default
class libraries.
}"
class APPLICATION
create
make
feature
make
do
-- These examples show non-console input (a plain text file)
-- with end-of-input handling.
read_lines_from_file
read_words_from_file
-- These examples use simplified input from 'io', that
-- handles the details of whether it's stdin or not
-- They terminate on a line (word) of "q"
read_lines_from_console_with_termination
read_words_from_console_with_termination
-- The next examples show reading stdin explicitly
-- as if it were a text file. It expects and end of file
-- termination and so will loop indefinitely unless reading
-- from a pipe or your console can send an EOF.
read_lines_from_stdin
read_words_from_stdin
-- These examples use simplified input from 'io', that
-- handles the details of whether it's stdin or not,
-- but have no explicit termination
read_lines_from_console_forever
read_words_from_console_forever
end
--|--------------------------------------------------------------
read_lines_from_file
-- Read input from a text file
-- Echo each line of the file to standard output.
--
-- Some language examples omit file open/close operations
-- but are included here for completeness. Additional error
-- checking would be appropriate in production code.
local
tf: PLAIN_TEXT_FILE
do
print ("Reading lines from a file%N")
create tf.make ("myfile") -- Create a file object
tf.open_read -- Open the file in read mode
-- The actual input loop
from
until tf.end_of_file
loop
tf.read_line
print (tf.last_string + "%N")
end
tf.close -- Close the file
end
--|--------------------------------------------------------------
read_words_from_file
-- Read input from a text file
-- Echo each word of the file to standard output on a
-- separate line.
--
-- Some language examples omit file open/close operations
-- but are included here for completeness. Additional error
-- checking would be appropriate in production code.
local
tf: PLAIN_TEXT_FILE
do
print ("Reading words from a file%N")
create tf.make ("myfile") -- Create a file object
tf.open_read -- Open the file in read mode
-- The actual input loop
from
until tf.end_of_file
loop
-- This instruction is the only difference between this
-- example and the read_lines_from_file example
tf.read_word
print (tf.last_string + "%N")
end
tf.close -- Close the file
end
--|--------------------------------------------------------------
read_lines_from_console_with_termination
-- Read lines from console and echo them back to output
-- until the line contains only the termination key 'q'
--
-- 'io' is acquired through inheritance from class ANY,
-- the top of all inheritance hierarchies.
local
the_cows_come_home: BOOLEAN
do
print ("Reading lines from console%N")
from
until the_cows_come_home
loop
io.read_line
if io.last_string ~ "q" then
the_cows_come_home := True
print ("Mooooo!%N")
else
print (io.last_string)
io.new_line
end
end
end
--|--------------------------------------------------------------
read_words_from_console_with_termination
-- Read words from console and echo them back to output, one
-- word per line, until the line contains only the
-- termination key 'q'
--
-- 'io' is acquired through inheritance from class ANY,
-- the top of all inheritance hierarchies.
local
the_cows_come_home: BOOLEAN
do
print ("Reading words from console%N")
from
until the_cows_come_home
loop
io.read_word
if io.last_string ~ "q" then
the_cows_come_home := True
print ("Mooooo!%N")
else
print (io.last_string)
io.new_line
end
end
end
--|--------------------------------------------------------------
read_lines_from_console_forever
-- Read lines from console and echo them back to output
-- until the program is terminated externally
--
-- 'io' is acquired through inheritance from class ANY,
-- the top of all inheritance hierarchies.
do
print ("Reading lines from console (no termination)%N")
from
until False
loop
io.read_line
print (io.last_string + "%N")
end
end
--|--------------------------------------------------------------
read_words_from_console_forever
-- Read words from console and echo them back to output, one
-- word per line until the program is terminated externally
--
-- 'io' is acquired through inheritance from class ANY,
-- the top of all inheritance hierarchies.
do
print ("Reading words from console (no termination)%N")
from
until False
loop
io.read_word
print (io.last_string + "%N")
end
end
--|--------------------------------------------------------------
read_lines_from_stdin
-- Read input from a stream on standard input
-- Echo each line of the file to standard output.
-- Note that we treat standard input as if it were a plain
-- text file
local
tf: PLAIN_TEXT_FILE
do
print ("Reading lines from stdin (EOF termination)%N")
tf := io.input
from
until tf.end_of_file
loop
tf.read_line
print (tf.last_string + "%N")
end
end
--|--------------------------------------------------------------
read_words_from_stdin
-- Read input from a stream on standard input
-- Echo each word of the file to standard output on a new
-- line
-- Note that we treat standard input as if it were a plain
-- text file
local
tf: PLAIN_TEXT_FILE
do
print ("Reading words from stdin (EOF termination)%N")
tf := io.input
from
until tf.end_of_file
loop
tf.read_line
print (tf.last_string + "%N")
end
end
end
Elena
ELENA 6.x:
Using ReaderEnumerator
import system'routines;
import system'io;
import extensions'routines;
public program()
{
ReaderEnumerator.new(File.assign("file.txt")).forEach(printingLn)
}
Using loop statement
import system'io;
public program()
{
using(var reader := File.assign("file.txt").textreader())
{
while (reader.Available)
{
console.writeLine(reader.readLine())
}
}
}
Elixir
defmodule RC do
def input_loop(stream) do
case IO.read(stream, :line) do
:eof -> :ok
data -> IO.write data
input_loop(stream)
end
end
end
path = hd(System.argv)
File.open!(path, [:read], fn stream -> RC.input_loop(stream) end)
Erlang
% Implemented by Arjun Sunel
-module(read_files).
-export([main/0]).
main() ->
Read = fun (Filename) -> {ok, Data} = file:read_file(Filename), Data end,
Lines = string:tokens(binary_to_list(Read("read_files.erl")), "\n"),
lists:foreach(fun (Y) -> io:format("~s~n", [Y]) end, lists:zipwith(fun(X,_)->X end, Lines, lists:seq(1, length(Lines)))).
ERRE
input from stdio
LOOP INPUT(LINE,A$) PRINT(A$) EXIT IF <condition> ! condition to be implemented to ! to avoid and endless loop END LOOP
reading a text file line by line
OPEN("I",#1,FILENAME$) WHILE NOT EOF(1) INPUT(LINE,#1,A$) PRINT(A$) END WHILE CLOSE(1)
Note: with GET(#1) you can read character by character.
Euphoria
Process text stream line-by-line:
procedure process_line_by_line(integer fn)
object line
while 1 do
line = gets(fn)
if atom(line) then
exit
end if
-- process the line
end while
end procedure
F#
Using a sequence expression:
let lines_of_file file =
seq { use stream = System.IO.File.OpenRead file
use reader = new System.IO.StreamReader(stream)
while not reader.EndOfStream do
yield reader.ReadLine() }
The file is reopened every time the sequence is traversed and lines are read on-demand so this can handle arbitrarily-large files.
Factor
"file.txt" utf8 [ [ process-line ] each-line ] with-file-reader
Fantom
An input stream can be from a string or from a file. The method eachLine
will divide the stream by linebreaks. The method readStrToken
takes two arguments: a maximum size to read, and a function to decide when to stop reading - by default, it stops when it finds a white space.
class Main
{
public static Void main ()
{
// example of reading by line
str := "first\nsecond\nthird\nword"
inputStream := str.in
inputStream.eachLine |Str line|
{
echo ("Line is: $line")
}
// example of reading by word
str = "first second third word"
inputStream = str.in
word := inputStream.readStrToken // reads up to but excluding next space
while (word != null)
{
echo ("Word: $word")
inputStream.readChar // skip over the preceding space!
word = inputStream.readStrToken
}
}
}
Forth
4096 constant max-line
: read-lines
begin stdin pad max-line read-line throw
while pad swap \ addr len is the line of data, excluding newline
2drop
repeat ;
Fortran
The code read line-by-line, but the maximum length of the line is limited (by a parameter)
program BasicInputLoop
implicit none
integer, parameter :: in = 50, &
linelen = 1000
integer :: ecode
character(len=linelen) :: l
open(in, file="afile.txt", action="read", status="old", iostat=ecode)
if ( ecode == 0 ) then
do
read(in, fmt="(A)", iostat=ecode) l
if ( ecode /= 0 ) exit
write(*,*) trim(l)
end do
close(in)
end if
end program BasicInputLoop
FreeBASIC
' FB 1.05.0 Win64
Dim line_ As String ' line is a keyword
Open "input.txt" For Input As #1
While Not Eof(1)
Input #1, line_
Print line_ ' echo to the console
Wend
Close #1
Print
Print "Press any key to quit"
Sleep
Frink
while (line = readStdin[]) != undef
println[line]
FutureBasic
Note: This code goes beyond simply specifying the file to open. It includes a dialog window that allows the user to select a text file to read. The entire contents of the file are read in at once, rather than line by line.
include "NSLog.incl"
local fn ReadTextFile
CFURLRef url
CFStringRef string
url = openpanel 1, @"Select text file..."
if ( url )
string = fn StringWithContentsOfURL( url, NSUTF8StringEncoding, NULL )
if ( string )
NSLog(@"%@",string)
end if
else
// user cancelled
end if
end fn
fn ReadTextFile
HandleEvents
GDScript
extends MainLoop
func _process(_delta: float) -> bool:
while true:
# Read a line from stdin
var input: String = OS.read_string_from_stdin()
# Empty lines are "\n" whereas end of input will be completely empty.
if len(input) == 0:
break
printraw(input)
return true # Exit
gnuplot
The following gnuplot script echoes standard input to standard output line-by-line until the end of the stream.
!cat
It makes use of the ability of gnuplot to spawn shell commands. In that sense it might be considered cheating. Nevertheless, this is a valid gnuplot script that does meet the requirements of the task description.
It seems impossible to complete this task with just standard gnuplot commands.
Go
The following reads a line at a time from stdin.
package main
import (
"bufio"
"io"
"log"
"os"
)
func main() {
in := bufio.NewReader(os.Stdin)
for {
s, err := in.ReadString('\n')
if err != nil {
// io.EOF is expected, anything else
// should be handled/reported
if err != io.EOF {
log.Fatal(err)
}
break
}
// Do something with the line of text
// in string variable s.
_ = s
}
}
Or, using bufio.Scanner
you can read
line at a time,
word at a time,
byte or Unicode code point at a time,
or by any custom "split function".
package main
import (
"bufio"
"log"
"os"
)
func main() {
s := bufio.NewScanner(os.Stdin)
// Select the split function, other ones are available
// in bufio or you can provide your own.
s.Split(bufio.ScanWords)
for s.Scan() {
// Get and use the next 'token'
asBytes := s.Bytes() // Bytes does no alloaction
asString := s.Text() // Text returns a newly allocated string
_, _ = asBytes, asString
}
if err := s.Err(); err != nil {
// Handle/report any error (EOF will not be reported)
log.Fatal(err)
}
}
Groovy
Solution:
def lineMap = [:]
System.in.eachLine { line, i ->
lineMap[i] = line
}
lineMap.each { println it }
- Test:
$ groovy -e 'def lineMap = [:] > System.in.eachLine { line, i -> > lineMap[i] = line > } > lineMap.each { println it }' <<EOF > > Whose woods these are I think I know > His house is in the village tho' > He will not see me stopping here > To watch his woods fill up with snow > EOF
- Output:
1= 2=Whose woods these are I think I know 3=His house is in the village tho' 4=He will not see me stopping here 5=To watch his woods fill up with snow
Haskell
The whole contents of a file can be read lazily. The standard functions lines and words convert that lazily into the lists of lines resp. words. Usually, one wouldn't use extra routines for that, but just use readFile and then put 'lines' or words somewhere in the next processing step.
import System.IO
readLines :: Handle -> IO [String]
readLines h = do
s <- hGetContents h
return $ lines s
readWords :: Handle -> IO [String]
readWords h = do
s <- hGetContents h
return $ words s
HicEst
CHARACTER name='myfile.txt', string*1000
OPEN(FIle=name, OLD, LENgth=bytes, IOStat=errorcode, ERror=9)
DO line = 1, bytes ! loop terminates with end-of-file error at the latest
READ(FIle=name, IOStat=errorcode, ERror=9) string
WRITE(StatusBar) string
ENDDO
9 WRITE(Messagebox, Name) line, errorcode
i
software {
loop {
read()
errors {
exit
}
}
}
Icon and Unicon
See str2toks
J
Script "read-input-until-eof.ijs":
#!/usr/bin/ijconsole
NB. read input until EOF
((1!:1) 3)(1!:2) 4 NB. tested under j602
exit ''
Example:
$ ./read-input-to-eof.ijs <<EOF
> abc
> def
> ghi
> now is the time for all good men ...
> EOF
abc
def
ghi
now is the time for all good men ...
Java
Some people prefer Scanner or BufferedReader, so a way with each is presented.
import java.io.InputStream;
import java.util.Scanner;
public class InputLoop {
public static void main(String args[]) {
// To read from stdin:
InputStream source = System.in;
/*
Or, to read from a file:
InputStream source = new FileInputStream(filename);
Or, to read from a network stream:
InputStream source = socket.getInputStream();
*/
Scanner in = new Scanner(source);
while(in.hasNext()){
String input = in.next(); // Use in.nextLine() for line-by-line reading
// Process the input here. For example, you could print it out:
System.out.println(input);
}
}
}
Or
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
public class InputLoop {
public static void main(String args[]) {
// To read from stdin:
Reader reader = new InputStreamReader(System.in);
/*
Or, to read from a file:
Reader reader = new FileReader(filename);
Or, to read from a network stream:
Reader reader = new InputStreamReader(socket.getInputStream());
*/
try {
BufferedReader inp = new BufferedReader(reader);
while(inp.ready()) {
int input = inp.read(); // Use in.readLine() for line-by-line
// Process the input here. For example, you can print it out.
System.out.println(input);
}
} catch (IOException e) {
// There was an input error.
}
}
}
JavaScript
These implementations of JavaScript define a readline()
function, so:
$ js -e 'while (line = readline()) { do_something_with(line); }' < inputfile
As above, this operates on standard input
var text_stream = WScript.StdIn;
var i = 0;
while ( ! text_stream.AtEndOfStream ) {
var line = text_stream.ReadLine();
// do something with line
WScript.echo(++i + ": " + line);
}
jq
The jq program for reading and writing is simply the one-character program:
.
For example, to echo each line of text in a file, one could invoke jq as follows:
jq -r -R . FILENAME
If the input file consists of well-formed JSON entities (including scalars), then the following invocation could be used to "pretty-print" the input:
jq . FILENAME
Other options, e.g. to emit JSON in compact form, also exist.
Jsish
/* Input loop in Jsish */
var line;
var cs = 0, ls = 0;
while (line = console.input()) { cs += line.length; ls += 1; }
printf("%d lines, %d characters\n", ls, cs);
- Output:
prompt$ jsish input-loop.jsi <input-loop.jsi 8 lines, 159 characters
Julia
We create a text stream and read the lines from the stream one by one, printing them on screen. Note that the lines end by a newline, except the last one. The ending newlines are part of the strings returned by the function readline. Once the end of the stream is reached, readline returns an empty string.
stream = IOBuffer("1\n2\n3\n4\n\n6")
while !eof(stream)
line = readline(stream)
println(line)
end
- Output:
1 2 3 4 6
Kotlin
// version 1.1
import java.util.*
fun main(args: Array<String>) {
println("Keep entering text or the word 'quit' to end the program:")
val sc = Scanner(System.`in`)
val words = mutableListOf<String>()
while (true) {
val input: String = sc.next()
if (input.trim().toLowerCase() == "quit") {
if (words.size > 0) println("\nYou entered the following words:\n${words.joinToString("\n")}")
return
}
words.add(input)
}
}
Sample input/output:
- Output:
Keep entering text or the word 'quit' to end the program: The quick brown fox jumps over the lazy dog quit You entered the following words: The quick brown fox jumps over the lazy dog
Lang5
: read-lines do read . "\n" . eof if break then loop ;
: ==>contents
'< swap open 'fh set fh fin read-lines fh close ;
'file.txt ==>contents
Lasso
local(
myfile = file('//path/to/file.txt'),
textresult = array
)
#myfile -> foreachline => {
#textresult -> insert(#1)
}
#textresult -> join('<br />')
Result: This is line one I am the second line Here is line 3
Liberty BASIC
filedialog "Open","*.txt",file$
if file$="" then end
open file$ for input as #f
while not(eof(#f))
line input #f, t$
print t$
wend
close #f
end
LIL
From the canread.lil sample that ships with LIL.
#
# canread test (note that canread is not available in LIL/FPLIL itself
# but provided by the command line interfaces in main.c/lil.pas)
#
# You can either call this and enter lines directly (use Ctrl+Z/Ctrl+D
# to finish) or a redirect (e.g. lil canread.lil < somefile.txt)
#
# Normally this is how you are supposed to read multiple lines from
# the standard input using the lil executable. For an alternative way
# that uses error catching via try see the eoferror.lil script.
#
set count 0
while {[canread]} {
readline
inc count
}
print $count lines
- Output:
prompt$ lil canread.lil <canread.lil 19 lines
Logo
There are several words which will return a line of input.
- readline - returns a line as a list of words
- readword - returns a line as a single word, or an empty list if it reached the end of file
- readrawline - returns a line as a single word, with no characters escaped
while [not eof?] [print readline]
LSL
LSL doesn't have a File System, but it does have Notecards that function as read-only text files, and can be use as configuration files or data sources.
To test it yourself; rez a box on the ground, add the following as a New Script, create a notecard named "Input_Loop_Data_Source.txt", and put what ever data you want in it (in this case I just put a copy of the source code.)
string sNOTECARD = "Input_Loop_Data_Source.txt";
default {
integer iNotecardLine = 0;
state_entry() {
llOwnerSay("Reading '"+sNOTECARD+"'");
llGetNotecardLine(sNOTECARD, iNotecardLine);
}
dataserver(key kRequestId, string sData) {
if(sData==EOF) {
llOwnerSay("EOF");
} else {
llOwnerSay((string)iNotecardLine+": "+sData);
llGetNotecardLine(sNOTECARD, ++iNotecardLine);
}
}
}
- Output:
Reading 'Input_Loop_Data_Source.txt' 0: string sNOTECARD = "Input_Loop_Data_Source.txt"; 1: default { 2: integer iNotecardLine = 0; 3: state_entry() { 4: llOwnerSay("Reading '"+sNOTECARD+"'"); 5: llGetNotecardLine(sNOTECARD, iNotecardLine); 6: } 7: dataserver(key kRequestId, string sData) { 8: if(sData==EOF) { 9: llOwnerSay("EOF"); 10: } else { 11: llOwnerSay((string)iNotecardLine+": "+sData); 12: llGetNotecardLine(sNOTECARD, ++iNotecardLine); 13: } 14: } 15: } EOF
Lua
lines = {}
str = io.read()
while str do
table.insert(lines,str)
str = io.read()
end
Via generic for loop
Reads line-by-line via an iterator (from stdin). Substitute io.lines()
with io.open(filename, "r"):lines()
to read from a file.
lines = {}
for line in io.lines() do
table.insert(lines, line) -- add the line to the list of lines
end
M2000 Interpreter
Document A$={1st Line
2nd line
3rd line
}
Save.Doc A$, "test.txt", 0 ' 0 for utf16-le
\\ we use Wide for utf16-le \\ without it we open using ANSI
Open "test.txt" For Wide Input Exclusive as #N
While Not Eof(#N) {
Line Input #N, ThisLine$
Print ThisLine$
}
Close #N
Clear A$
Load.Doc A$, "test.txt"
\\ print proportional text, all lines
Report A$
\\ Print one line, non proportional
\\ using paragraphs
For i=0 to Doc.par(A$)-1
Print Paragraph$(A$, i)
Next i
\\ List of current variables (in any scope, public only)
List
Maple
readinput:=proc(filename)
local line,file;
file:="";
line:=readline(filename);
while line<>0 do
line:=readline(filename);
file:=cat(file,line);
end do;
end proc;
MACRO-10
TITLE Input Loop
COMMENT !
Input-loop example, PDP-10 assembly language, kjx 2022.
Assembler: MACRO-10 Operating system: TOPS-20
This program opens the file "test.txt" and prints it's contents.
Note that the PDP-10 is a word-addressable machine with 36bit words,
and text-files use 7bit ASCII. That means one word fits 5 characters
and the buffer length of 20 (octal!) makes room for 80 (decimal) char-
acters.
The system-call used, SIN%, can also read until a specific character
(like CR) is seen, so reading one-line-at-a-time is also possible with
minimal changes.
!
SEARCH MONSYM,MACSYM
.REQUIRE SYS:MACREL
STDAC. ;Define standard register names.
;;
;; Buffers and data:
;;
PDLLEN==20
PDL: BLOCK PDLLEN ;Callstack (for ERCAL later).
MAXBUF==20 ;Use 20 36bit words for buffer.
MAXCHR==MAXBUF*5 ;One 36bit words fits 5 7bit bytes.
IN.BUF: BLOCK MAXBUF ;Buffer for lines read from file.
IN.JFN: BLOCK 1 ;Space for file-descriptor (JFN)
;;
;; Execution begins here:
;;
GO:: RESET% ;Initialize process.
MOVE P,[IOWD PDLLEN,PDL] ;Initialize stack.
;;
;; To open a file, we first have to get a file-handle (JFN):
;;
MOVX T1,GJ%OLD+GJ%SHT ;Flags go into T1:
; GJ%OLD = File already exists.
; GJ%SHT = Short system call.
HRROI T2,[ASCIZ /test.txt/] ;File name goes to T2.
GTJFN% ;Get a JFN for this file.
ERJMPS ERROR ; On error jump to ERROR.
MOVEM T1,IN.JFN ;Save the JFN.
;;
;; Now we can open the file:
;;
HRRZ T1,IN.JFN ;Right half-word of JFN into T1 +
;zero out left half to get rid of
;flags returned by GTJFN%.
MOVX T2,FLD(7,OF%BSZ)+OF%RD ;7bit characters, read-only.
OPENF% ;Open file.
ERJMPS ERROR ; On error jump to ERROR.
;;
;; Now we're reading MAXCHR 7bit bytes from that file
;; into IN.BUF and print that. If we'll see EOF, Q1 is
;; set to 1, and the loop is exited.
;;
SETZ Q1 ;Clear Q1 (we use that as an EOF marker)
DO.
MOVE T1,IN.JFN ;Read from IN.JFN (opened file)
HRROI T2,IN.BUF ;and write into IN.BUF.
MOVNI T3,MAXCHR ;Read maximum of MAXCHR characters.
SIN% ;Read string from file.
ERCAL [ MOVE T1,IN.JFN ; Error occured...
GTSTS% ; Get file status.
TXNN T2,GS%EOF ; End-of-file reached?
JRST ERROR ; No: Something else happened...
MOVEI Q1,1 ; Yes: Set Q1 to 1.
RET ] ; Continue.
MOVEI T1,.PRIOU ;Write to standard output.
HRROI T2,IN.BUF ;Buffer to write.
ADDI T3,MAXCHR ;Substract chars read from
MOVN T3,T3 ;maximum buffer length.
SOUT% ;Print string.
CAIN Q1,1 ;Is Q1 == 1?
JRST ENDPRG ; Yes: EOF was found above.
LOOP. ;Q1 is 0, so no EOF, continue.
ENDDO.
;;
;; ENDPRG: Close file and halt.
;;
ENDPRG: HRRZ T1,IN.JFN ;Put JFN into T1.
CLOSF% ;Close that file.
ERJMPS ERROR ; Go print error-msgs on error.
HALTF% ;Halt program.
JRST GO ;Allow for 'continue'-command.
;;
;; ERROR: Print standardized error-message and halt.
;;
ERROR: MOVEI T1,.PRIOU ;Print error on terminal (.PRIOU)
MOVE T2,[.FHSLF,,-1] ;Own process, most recent error.
SETZ T3, ;No limit on size of message.
ERSTR% ;Print that error message.
JFCL ; Ignore errors from ERSTR.
JFCL ; dito.
HALTF% ;Stop program.
JRST GO ;Allow for continuation.
END GO
Mathematica / Wolfram Language
stream = OpenRead["file.txt"];
While[a != EndOfFile, Read[stream, Word]];
Close[stream]
MAXScript
this function will read a file line by line.
fn ReadAFile FileName =
(
local in_file = openfile FileName
while not eof in_file do
(
--Do stuff in here--
print (readline in_file)
)
close in_file
)
Mercury
:- module input_loop.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
main(!IO) :-
io.stdin_stream(Stdin, !IO),
io.stdout_stream(Stdout, !IO),
read_and_print_lines(Stdin, Stdout, !IO).
:- pred read_and_print_lines(io.text_input_stream::in,
io.text_output_stream::in, io::di, io::uo) is det.
read_and_print_lines(InFile, OutFile, !IO) :-
io.read_line_as_string(InFile, Result, !IO),
(
Result = ok(Line),
io.write_string(OutFile, Line, !IO),
read_and_print_lines(InFile, OutFile, !IO)
;
Result = eof
;
Result = error(IOError),
Msg = io.error_message(IOError),
io.stderr_stream(Stderr, !IO),
io.write_string(Stderr, Msg, !IO),
io.set_exit_status(1, !IO)
).
mIRC Scripting Language
var %n = 1
while (%n <= $lines(input.txt)) {
write output.txt $read(input.txt,%n)
inc %n
}
ML/I
The very nature of ML/I is that its default behaviour is to copy from input to output until it reaches end of file.
Modula-2
PROCEDURE ReadName (VAR str : ARRAY OF CHAR);
VAR n : CARDINAL;
ch, endch : CHAR;
BEGIN
REPEAT
InOut.Read (ch);
Exhausted := InOut.EOF ();
IF Exhausted THEN RETURN END
UNTIL ch > ' '; (* Eliminate whitespace *)
IF ch = '[' THEN endch := ']' ELSE endch := ch END;
n := 0;
REPEAT
InOut.Read (ch);
Exhausted := InOut.EOF ();
IF Exhausted THEN RETURN END;
IF n <= HIGH (str) THEN str [n] := ch ELSE ch := endch END;
INC (n)
UNTIL ch = endch;
IF n <= HIGH (str) THEN str [n-1] := 0C END;
lastCh := ch
END ReadName;
Modula-3
MODULE Output EXPORTS Main;
IMPORT Rd, Wr, Stdio;
VAR buf: TEXT;
<*FATAL ANY*>
BEGIN
WHILE NOT Rd.EOF(Stdio.stdin) DO
buf := Rd.GetLine(Stdio.stdin);
Wr.PutText(Stdio.stdout, buf);
END;
END Output.
NetRexx
Using NetRexx ASK Special Variable
/* NetRexx */
options replace format comments java crossref symbols nobinary
-- Read from default input stream (console) until end of data
lines = ''
lines[0] = 0
lineNo = 0
loop label ioloop forever
inLine = ask
if inLine = null then leave ioloop -- stop on EOF (Try Ctrl-D on UNIX-like systems or Ctrl-Z on Windows)
lineNo = lineNo + 1
lines[0] = lineNo
lines[lineNo] = inLine
end ioloop
loop l_ = 1 to lines[0]
say l_.right(4)':' lines[l_]
end l_
return
Using Java Scanner
/* NetRexx */
options replace format comments java crossref symbols nobinary
-- Read from default input stream (console) until end of data
lines = ''
lines[0] = 0
inScanner = Scanner(System.in)
loop l_ = 1 while inScanner.hasNext()
inLine = inScanner.nextLine()
lines[0] = l_
lines[l_] = inLine
end l_
inScanner.close()
loop l_ = 1 to lines[0]
say l_.right(4)':' lines[l_]
end l_
return
Nim
Read from stdin until end of data has been reached:
var line = ""
while stdin.readLine(line):
echo line
Choose splitting string:
import strutils
var lines = stdin.readAll()
for line in lines.split("\n"):
echo line
Read from a known text file:
var i = open("input.txt")
for line in i.lines:
discard # process line
i.close()
Another, shorter, way to do this:
for line in "input.text".lines:
discard # process line
NodeJS
#!/usr/bin/env node
const EventEmitter = require('events');
function stdinLineByLine() {
const stdin = new EventEmitter();
let buff = '';
process.stdin
.on('data', data => {
buff += data;
lines = buff.split(/\r\n|\n/);
buff = lines.pop();
lines.forEach(line => stdin.emit('line', line));
})
.on('end', () => {
if (buff.length > 0) stdin.emit('line', buff);
});
return stdin;
}
const stdin = stdinLineByLine();
stdin.on('line', console.log);
Oberon-2
Works with oo2c Version 2
MODULE InputLoop;
IMPORT
StdChannels,
Channel;
VAR
reader: Channel.Reader;
writer: Channel.Writer;
c: CHAR;
BEGIN
reader := StdChannels.stdin.NewReader();
writer := StdChannels.stdout.NewWriter();
reader.ReadByte(c);
WHILE reader.res = Channel.done DO
writer.WriteByte(c);
reader.ReadByte(c)
END
END InputLoop.
Execute: InputLoop < Inputloop.Mod
Output:
MODULE InputLoop; IMPORT StdChannels, Channel; VAR reader: Channel.Reader; writer: Channel.Writer; c: CHAR; BEGIN reader := StdChannels.stdin.NewReader(); writer := StdChannels.stdout.NewWriter(); reader.ReadByte(c); WHILE reader.res = Channel.done DO writer.WriteByte(c); reader.ReadByte(c) END END InputLoop.
Objeck
use IO;
bundle Default {
class Test {
function : Main(args : System.String[]) ~ Nil {
f := FileReader->New("in.txt");
if(f->IsOpen()) {
string := f->ReadString();
while(string->Size() > 0) {
string->PrintLine();
string := f->ReadString();
};
f->Close();
};
}
}
}
OCaml
let rec read_lines ic =
try
let line = input_line ic in
line :: read_lines ic
with End_of_file ->
[]
The version above will work for small files, but it is not tail-recursive.
Below will be more scalable:
let read_line_opt ic =
try Some (input_line ic)
with End_of_file -> None
let read_lines ic =
let rec loop acc =
match read_line_opt ic with
| Some line -> loop (line :: acc)
| None -> (List.rev acc)
in
loop []
;;
Or with a higher order function:
let read_lines f ic =
let rec loop () =
try f (input_line ic); loop ()
with End_of_file -> ()
in
loop ()
let () =
let ic = open_in Sys.argv.(1) in
read_lines print_endline ic
Oforth
Reads a file line by line and write each line on standard output :
: readFile(filename) File new(filename) apply(#println) ;
OmniMark
This OmniMark script will output the file text.txt verbatim, reading it one line at a time:
;command line: omnimark -sb Input-loop.xom text.txt
process
repeat scan file #args
match any-text+ => line white-space
output '%x(line)%n'
again
That would be less common than using #main-input and find rule(s), noting the following effectively produces the same result:
;command line: omnimark -sb Input-loop.xom text.txt
process
submit #main-input
find any-text+ => line
output line
Oz
%% Returns a list of lines.
%% Text: an instance of Open.text (a mixin class)
fun {ReadAll Text}
case {Text getS($)} of false then nil
[] Line then Line|{ReadAll Text}
end
end
Pascal
{ for stdio }
var
s : string ;
begin
repeat
readln(s);
until s = "" ;
{ for a file }
var
f : text ;
s : string ;
begin
assignfile(f,'foo');
reset(f);
while not eof(f) do
readln(f,s);
closefile(f);
end;
PascalABC.NET
begin
var f := OpenRead('a.txt');
while not f.Eof do
Println(f.ReadString);
f.Close;
end.
Perl
The angle brackets operator ( <...> ) reads one line at a time from a filehandle in scalar context:
open FH, "< $filename" or die "can't open file: $!";
while (my $line = <FH>) {
chomp $line; # removes trailing newline
# process $line
}
close FH or die "can't close file: $!";
Or you can get a list of all lines when you use it in list context:
@lines = <FH>;
Or a simpler program for lines of files entered as command line arguments or standard input:
while (<>) {
# $_ contains a line
}
Invoking perl with the -p or -n option implies the above loop, executing its code once per input line, with the line stored in $_. -p will print $_ automatically at the end of each iteration, -n will not.
$ seq 5 | perl -pe '$_ = "Hello $_"' Hello 1 Hello 2 Hello 3 Hello 4 Hello 5 $ seq 5 | perl -ne 'print "Hello $_"' Hello 1 Hello 2 Hello 3 Hello 4 Hello 5
Phix
Process text stream line-by-line:
without js -- (file i/o) procedure process_line_by_line(integer fn) object line while 1 do line = gets(fn) if atom(line) then exit end if -- process the line end while end procedure
Phixmonti
/# Rosetta Code problem: http://rosettacode.org/wiki/Input_loop
by Galileo, 10/2022 #/
include ..\Utilitys.pmt
def eof? dup -1 == enddef
"input.txt" "r" fopen
( inf 0 -1 ) for drop
dup fgets eof? if drop exitfor else print endif
endfor
fclose
"input.txt" "r" fopen
eof? not while
dup fgets eof? if drop false else print true endif
endwhile
fclose
PHP
$fh = fopen($filename, 'r');
if ($fh) {
while (!feof($fh)) {
$line = rtrim(fgets($fh)); # removes trailing newline
# process $line
}
fclose($fh);
}
Or you can get an array of all the lines in the file:
$lines = file($filename);
Or you can get the entire file as a string:
$contents = file_get_contents($filename);
Picat
main =>
Reader = open("file.txt"),
while(not at_end_of_stream(Reader))
L = read_line(Reader),
println(L)
end,
close(Reader).
PicoLisp
This reads all lines in a file, and returns them as a list of lists
(in "file.txt"
(make
(until (eof)
(link (line)) ) ) )
PL/I
declare line character (200) varying;
open file (in) title ('/TEXT.DAT,type(text),recsize(200)' );
on endfile (in) stop;
do forever;
get file(in) edit (line) (L);
put skip list (line);
end;
PowerShell
Get-Content c:\file.txt |
ForEach-Object {
$_
}
or
ForEach-Object -inputobject (get-content c:\file.txt) {$_}
PureBasic
File objects can be read bytewise, characterwise (ASCII or UNICODE), floatwise, doublewise, integerwise, linewise ...
If OpenConsole()
; file based line wise
If ReadFile(0, "Text.txt")
While Eof(0) = 0
Debug ReadString(0) ; each line until eof
Wend
CloseFile(0)
EndIf
; file based byte wise
If ReadFile(1, "Text.bin")
While Eof(1) = 0
Debug ReadByte(1) ; each byte until eof
Wend
CloseFile(1)
EndIf
EndIf
Python
To create a Python3 input loop use python's `input()` function.
while(True):
x = input("What is your age? ")
print(x)
Python file objects can be iterated like lists:
my_file = open(filename, 'r')
try:
for line in my_file:
pass # process line, includes newline
finally:
my_file.close()
One can open a new stream for read and have it automatically close when done, with a new "with" statement:
#from __future__ import with_statement # is not needed in Python 3.6
with open(filename, 'r') as f:
for line in f:
pass # process line, includes newline
You can also get lines manually from a file:
line = my_file.readline() # returns a line from the file
lines = my_file.readlines() # returns a list of the rest of the lines from the file
This does not mix well with the iteration, however.
When you want to read from stdin, or (multiple) filenames are given on the command line:
import fileinput
for line in fileinput.input():
pass # process line, includes newline
The fileinput module can also do inplace file editing, follow line counts, and the name of the current file being read etc.
R
Note that read.csv and read.table provide alternatives for files with 'dataset' style contents.
lines <- readLines("file.txt")
Racket
The following prints input lines from standard input to standard output:
#lang racket
(copy-port (current-input-port) (current-output-port))
Rapira
while 1 do
input text: str
output: str
od
Raku
(formerly Perl 6)
In Raku, filehandles etc. provide the .lines
and .words
methods which return lazy lists, and can thus they be iterated using a for
loop...
Line-by-line (line endings are automatically stripped)
- From a file:
for "filename.txt".IO.lines -> $line { ... }
- From standard input:
for $*IN.lines -> $line { ... }
- From a pipe:
for run(«find -iname *.txt», :out).out.lines -> $filename { ... }
- From a pipe, with custom line separator (in this example to handle filenames containing newlines):
for run(«find -iname *.txt -print0», :nl«\0», :out).out.lines -> $filename { ... }
Word-by-word
- From a file
for "filename.txt".IO.words -> $word { ... }
- From standard input or a pipe, accordingly.
REBOL
REBOL [
Title: "Basic Input Loop"
URL: http://rosettacode.org/wiki/Basic_input_loop
]
; Slurp the whole file in:
x: read %file.txt
; Bring the file in by lines:
x: read/lines %file.txt
; Read in first 10 lines:
x: read/lines/part %file.txt 10
; Read data a line at a time:
f: open/lines %file.txt
while [not tail? f][
print f/1
f: next f ; Advance to next line.
]
close f
REXX
version 1a
Reading line by line from the standard input using linein and lines did not work.
do while stream(stdin, "State") <> "NOTREADY"
call charout ,charin(stdin)
end
version 1b
Apparently only lines() does not work
Do Until input=''
input=linein(stdin)
Call lineout ,input
End
version 2
/* -- AREXX -- */
do until eof(stdin)
l = readln(stdin)
say l
end
version 3
Note that if REXX is reading from the default (console) input stream, there is no well-
defined e-o-f (end of file), so to speak.
Therefore, the following two REXX programs use the presence of a null line to indicate e-o-f.
/*REXX program reads from the (console) default input stream until null*/
do until _==''
parse pull _
end /*until*/ /*stick a fork in it, we're done.*/
version 4
/*REXX program reads from the (console) default input stream until null*/
do until _==''
_= linein()
end /*until*/ /*stick a fork in it, we're done.*/
Ring
fp = fopen("C:\Ring\ReadMe.txt","r")
r = fgetc(fp)
while isstring(r)
r = fgetc(fp)
if r = char(10) see nl
else see r ok
end
fclose(fp)
Ruby
Ruby input streams are IO objects. One can use IO#each or IO#each_line to iterate lines from a stream.
stream = $stdin
stream.each do |line|
# process line
end
IO objects are also Enumerable (like Array or Range), and have methods like Enumerable#map, which call IO#each to loop through lines from a stream.
# Create an array of lengths of every line.
ary = stream.map {|line| line.chomp.length}
To open a new stream for reading, see Read a file line by line#Ruby.
Run BASIC
open "\testFile.txt" for input as #f
while not(eof(#f))
line input #f, a$
print a$
wend
close #f
Rust
use std::io::{self, BufReader, Read, BufRead};
use std::fs::File;
fn main() {
print_by_line(io::stdin())
.expect("Could not read from stdin");
File::open("/etc/fstab")
.and_then(print_by_line)
.expect("Could not read from file");
}
fn print_by_line<T: Read>(reader: T) -> io::Result<()> {
let buffer = BufReader::new(reader);
for line in buffer.lines() {
println!("{}", line?)
}
Ok(())
}
Scala
scala.io.Source.fromFile("input.txt").getLines().foreach {
line => ... }
sed
Sed by default loops over each line and executes its given script on it:
$ seq 5 | sed '' 1 2 3 4 5
The automatic printing can be suppressed with -n, and performed manually with p:
$ seq 5 | sed -n p 1 2 3 4 5
Seed7
$ include "seed7_05.s7i";
const proc: main is func
local
var string: line is "";
begin
while hasNext(IN) do
readln(line);
writeln("LINE: " <& line);
end while;
end func;
Sidef
To read from the standard input, you can use STDIN as your fh.
var file = File(__FILE__)
file.open_r(\var fh, \var err) || die "#{file}: #{err}"
fh.each { |line| # iterates the lines of the fh
line.each_word { |word| # iterates the words of the line
say word
}
}
Slate
(File newNamed: 'README') reader sessionDo: [| :input | input lines do: [| :line | inform: line]].
Smalltalk
|f|
f := FileStream open: 'afile.txt' mode: FileStream read.
[ f atEnd ] whileFalse: [ (f nextLine) displayNl ] .
SNOBOL4
loop output = input :s(loop)
end
Sparkling
var line;
while (line = getline()) != nil {
print(line);
}
Standard ML
fun foldLines f init strm =
case TextIO.inputLine strm of
SOME line => foldLines f (f (line, init)) strm
| NONE => init
Example: Output the lines from stdin in reverse order.
val () = (print o foldLines op^ "") TextIO.stdIn
Tcl
set fh [open $filename]
while {[gets $fh line] != -1} {
# process $line
}
close $fh
For “small” files, it is often more common to do this:
set fh [open $filename]
set data [read $fh]
close $fh
foreach line [split $data \n] {
# process line
}
TUSCRIPT
$$ MODE TUSCRIPT
file="a.txt"
ERROR/STOP OPEN (file,READ,-std-)
ACCESS source: READ/RECORDS/UTF8 $file s,text
LOOP
READ/NEXT/EXIT source
PRINT text
ENDLOOP
ENDACCESS source
TypeScript
#!/usr/bin/env node
import EventEmitter from 'events';
function stdinLineByLine() {
const stdin = new EventEmitter();
let buff = '';
let lines;
process.stdin
.on('data', (data) => {
buff += data;
lines = buff.split(/\r\n|\n/);
buff = lines.pop();
lines.forEach((line) => stdin.emit('line', line));
})
.on('end', () => {
if (buff.length > 0) stdin.emit('line', buff);
});
return stdin;
}
const stdin = stdinLineByLine();
stdin.on('line', console.log);
UNIX Shell
When there is something to do with the input, here is a loop:
while read line ; do
# examine or do something to the text in the "line" variable
echo "$line"
done
The following echoes standard input to standard output line-by-line until the end of the stream.
cat < /dev/stdin > /dev/stdout
Since cat
defaults to reading from standard input and writing to standard output, this can be further simplified to the following.
cat
UnixPipes
the pipe 'yes XXX' produces a sequence
read by lines:
yes 'A B C D ' | while read x ; do echo -$x- ; done
read by words:
yes 'A B C D ' | while read -d\ a ; do echo -$a- ; done
Ursa
decl file f
f.open "filename.txt"
while (f.hasnext)
out (in string f) endl console
end while
Vala
int main() {
string? s;
while((s = stdin.read_line()) != null) {
stdout.printf("%s\n", s);
}
return 0;
}
VBA
Public Sub test()
Dim filesystem As Object, stream As Object, line As String
Set filesystem = CreateObject("Scripting.FileSystemObject")
Set stream = filesystem.OpenTextFile("D:\test.txt")
Do While stream.AtEndOfStream <> True
line = stream.ReadLine
Debug.Print line
Loop
stream.Close
End Sub
VBScript
filepath = "SPECIFY PATH TO TEXT FILE HERE"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInFile = objFSO.OpenTextFile(filepath,1,False,0)
Do Until objInFile.AtEndOfStream
line = objInFile.ReadLine
WScript.StdOut.WriteLine line
Loop
objInFile.Close
Set objFSO = Nothing
Visual Basic .NET
This reads a stream line by line, outputing each line to the screen.
Sub Consume(ByVal stream As IO.StreamReader)
Dim line = stream.ReadLine
Do Until line Is Nothing
Console.WriteLine(line)
line = stream.ReadLine
Loop
End Sub
V (Vlang)
import os
fn main() {
mut ay_view_content := []string{}
file := "./input.txt"
// check if file exists
if os.is_file(file) == false {
print("Error: '${file}' not found")
exit(-1)
}
ay_view_content << os.read_lines(file) or {print(err) exit(-2)}
for line in ay_view_content {
if line !="" {println(line)}
if line =="" {println("Found blank line!")}
}
}
Wren
import "io" for File
File.open("input.txt") { |file|
var offset = 0
var line = ""
while(true) {
var b = file.readBytes(1, offset)
offset = offset + 1
if (b == "\n") {
// process 'line'
line = "" // reset line variable
} else if (b == "\r") { // Windows
// wait for following "\n"
} else if (b == "") { // end of stream
return
} else {
line = line + b
}
}
}
x86 Assembly
GAS, 64 bit (Linux): Compiled with gcc -nostdlib
. Memory maps the file and outputs one line at a time. Try ./a.out file
, ./a.out < file
, or ./a.out <<< "Heredoc"
. It's a little like cat, but less functional.
#define SYS_WRITE $1
#define SYS_OPEN $2
#define SYS_CLOSE $3
#define SYS_FSTAT $5
#define SYS_MMAP $9
#define SYS_MUNMAP $11
#define SYS_EXIT $60
// From experiments:
#define FSIZEOFF 48
#define STATSIZE 144
// From Linux source:
#define RDONLY $00
#define PROT_READ $0x1
#define MAP_PRIVATE $0x02
#define STDIN $0
#define STDOUT $1
.global _start
.text
/* Details: */
/*
Remember: %rax(%rdi, %rsi, %rdx, %r10, %r8, %r9)
- Open a file (get its fd)
- int fd = open("filename", RDONLY)
- Get its filesize:
- fstat(fd, statstruct). 0 if ok. fsize at statstruct+48
- Then memory map it.
- void* vmemptr = mmap(vmemptr, fsize, PROT_READ, MAP_PRIVATE, fd, 0)
- Scan for newlines, print line.
- Keep going until done. Details at 11.
- Unmap memory
- munmap(vmemptr, filesize). 0 if ok.
- Exit
*/
.macro ERRCHECK code
cmpq $\code, %rax
je fs_error
.endm
/* Local stack notes:
0: int fd
4: void* vmemptr
12: void* head
20: void* lookahead
28: void* end
*/
_start:
// Open:
movq RDONLY, %rsi
// Filename ptr is on stack currently as argv[1]:
cmpq $1, (%rsp) // if argc is 1, default to stdin
jnz open_file
subq $36, %rsp // local stack
movl STDIN, (%rsp)
jmp fstat
open_file:
movq 16(%rsp), %rdi // argc(8), argv0(8) => rsp+16. filename
movq SYS_OPEN, %rax
syscall
ERRCHECK -1
subq $36, %rsp // local stack
movl %eax, (%rsp) // int fd = open(argv[1], RDONLY)
// fstat to get filesize
fstat:
movq $statstruct, %rsi
movl (%rsp), %edi // fd
movq SYS_FSTAT, %rax
syscall // fstat(fd, statstruct)
ERRCHECK -1
// mmap - don't forget to munmap.
mmap:
movq $0, %r9 // offset
movq (%rsp), %r8 // fd
movq MAP_PRIVATE, %r10
movq PROT_READ, %rdx
movq filesize, %rsi
movq (%rsp), %rdi // vmemptr
movq SYS_MMAP, %rax
syscall
ERRCHECK -1
movq %rax, 4(%rsp) // void* vmemptr = mmap(vmemptr, fsize, PROT_READ, MAP_PRIVATE, fd, 0)
/* Print lines */
movq %rax, 12(%rsp) // head = vmemptr
addq filesize, %rax
decq %rax
movq %rax, 28(%rsp) // end = vmemptr+filesize-1
scan_outer:
movq 12(%rsp), %rax
cmpq 28(%rsp), %rax
jge cleanup // if head >= end, done
movq %rax, %rbx // Using rbx as lookahead
scan_inner:
cmpq 28(%rsp), %rbx
jge writeline // if lookahead >= end, write the line.
cmpb $'\n, (%rbx)
jz writeline // if '\n'==*lookahead, write the line
incq %rbx
jmp scan_inner
writeline:
// write:
incq %rbx
movq %rbx, %rdx
subq 12(%rsp), %rdx // rdx <- lookahead-head
movq 12(%rsp), %rsi
movq STDOUT, %rdi
movq SYS_WRITE, %rax
syscall // write(stdout, head, lookahead-head)
safety:
movq %rbx, 12(%rsp) // head = lookahead.
jmp scan_outer
cleanup:
// munmap
movq filesize, %rsi
movq 4(%rsp), %rdi
movq SYS_MUNMAP, %rax
syscall // munmap(vmemptr, filesize)
cmpq $-1, %rax
je fs_error
// close
movl (%rsp), %edi
movq SYS_CLOSE, %rax
syscall // close(fd)
ERRCHECK -1
exit:
movq SYS_EXIT, %rax
xorq %rdi, %rdi // The exit code.
syscall
fs_error:
movq SYS_EXIT, %rax
movq $-1, %rdi
syscall // exit(-1)
.data
statstruct: // This struct is 144 bytes. Only want size (+48)
.zero FSIZEOFF
filesize: // 8 bytes.
.quad 0
.zero STATSIZE-FSIZEOFF+8
XPL0
Text stream is a file redirected on command line i.e: <file.txt
int C;
[repeat \read file
repeat \read line
repeat C:= ChIn(1); \read word
ChOut(0, C);
until (C<^A ! C>^z) & (C<^0 ! C>^9); \non-alphanumeric
until C < $20; \CR, LF, or EOF
until C = \EOF\ $1A;
]
zkl
Many objects support "stream of" concepts such as lines, characters, chunks. Some are File, Data (bit bucket), List, Console. Word by word isn't explicitly supported. If an object is stream-able, it supports methods like foreach, pump, apply, reduce, etc.
foreach line in (File("foo.txt")){...}
List(1,2,3).readln() // here, a "line" is a list element
Utils.Helpers.zipWith(False, // enumerate a file
fcn(n,line){"%3d: %s".fmt(n,line).print()},[1..],File("cmp.zkl"))
- Programming Tasks
- Text processing
- Selection/Short Circuit/Console Program Basics
- Basic language learning
- Streams
- Simple
- PARI/GP/Omit
- TI-89 BASIC/Omit
- AArch64 Assembly
- Action!
- Ada
- Aime
- ALGOL 68
- ALGOL W
- AmigaE
- APL
- ARM Assembly
- Arturo
- AutoHotkey
- AWK
- BASIC
- BASIC256
- OxygenBasic
- QBasic
- True BASIC
- Yabasic
- Applesoft BASIC
- BaCon
- BBC BASIC
- Commodore BASIC
- IS-BASIC
- UBasic/4tH
- SmallBASIC
- Batch File
- Bracmat
- C
- C sharp
- C++
- Clojure
- COBOL
- Common Lisp
- D
- Tango
- Delphi
- Déjà Vu
- EasyLang
- Eiffel
- Elena
- Elixir
- Erlang
- ERRE
- Euphoria
- F Sharp
- Factor
- Fantom
- Forth
- Fortran
- FreeBASIC
- Frink
- FutureBasic
- GDScript
- Gnuplot
- Go
- Groovy
- Haskell
- HicEst
- I
- Icon
- Unicon
- Icon Programming Library
- J
- Java
- JavaScript
- Jq
- Jsish
- Julia
- Kotlin
- Lang5
- Lasso
- Liberty BASIC
- LIL
- Logo
- LSL
- Lua
- M2000 Interpreter
- Maple
- MACRO-10
- Mathematica
- Wolfram Language
- MAXScript
- Mercury
- MIRC Scripting Language
- ML/I
- Modula-2
- Modula-3
- NetRexx
- Nim
- NodeJS
- Oberon-2
- Objeck
- OCaml
- Oforth
- OmniMark
- Oz
- Pascal
- PascalABC.NET
- Perl
- Phix
- Phixmonti
- PHP
- Picat
- PicoLisp
- PL/I
- PowerShell
- PureBasic
- Python
- R
- Racket
- Rapira
- Raku
- REBOL
- REXX
- Ring
- Ruby
- Run BASIC
- Rust
- Scala
- Sed
- Seed7
- Sidef
- Slate
- Smalltalk
- SNOBOL4
- Sparkling
- Standard ML
- Tcl
- TUSCRIPT
- TypeScript
- UNIX Shell
- UnixPipes
- Ursa
- Vala
- VBA
- VBScript
- Visual Basic .NET
- V (Vlang)
- Wren
- X86 Assembly
- XPL0
- Zkl
- Pages with too many expensive parser function calls