Jump to content

Read a specific line from a file: Difference between revisions

Added solution for Action!
(Added solution for Action!)
Line 24:
f.read_line()
print(f.read_line())</lang>
 
=={{header|Action!}}==
In the following solution the input file is loaded from H6 drive. Altirra emulator automatically converts CR/LF character from ASCII into 155 character in ATASCII charset used by Atari 8-bit computer when one from H6-H10 hard drive under DOS 2.5 is used.
<lang Action!>BYTE FUNC ReadLine(CHAR ARRAY fname CARD index CHAR ARRAY result)
CHAR ARRAY line(255)
CARD curr
BYTE status,dev=[1]
 
Close(dev)
Open(dev,fname,4)
curr=1 status=1
WHILE Eof(dev)=0
DO
InputSD(dev,line)
IF curr=index THEN
SCopy(result,line)
status=0
EXIT
FI
curr==+1
OD
Close(dev)
RETURN (status)
 
PROC Test(CHAR ARRAY fname CARD index)
CHAR ARRAY result(255)
BYTE status
 
PrintF("Reading %U line...%E",index)
status=ReadLine(fname,index,result)
IF status=0 THEN
IF result(0)=0 THEN
PrintF("%U line is empty.%E%E",index)
ELSE
PrintF("%U line is:%E""%S""%E%E",index,result)
FI
ELSEIF status=1 THEN
PrintF("File contains less than %U lines.%E%E",index)
FI
RETURN
 
PROC Main()
CHAR ARRAY fname="H6:READ__96.ACT"
 
PrintF("Reading ""%S""...%E%E",fname)
Test(fname,7)
Test(fname,24)
Test(fname,50)
RETURN</lang>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Read_a_specific_line_from_a_file.png Screenshot from Atari 8-bit computer]
<pre>
Reading "H6:READ__96.ACT"...
 
Reading 7 line...
7 line is:
" Open(dev,fname,4)"
 
Reading 24 line...
24 line is:
" BYTE status"
 
Reading 50 line...
File contains less than 50 lines.
</pre>
 
=={{header|Ada}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.