Read a file line by line: Difference between revisions

m (added comments for heards outlining respective functions used in the code.)
Line 2,453:
!:LineCount = !.LineCount + 1,
io.format("%d: %s", [i(!.LineCount), s(Line)], !IO).</lang>
 
=={{header|MIPS Assembly}}==
A bit cheaty, but you can include raw text using the <code>incbin</code> directive (or equivalent) and label it, and use that label in your code to get the address of the file and read it. Once you've done that, it's a simple matter of using either the carriage return (13) or line feed (10) as the terminator for printing.
 
<lang mips>.include "\SrcAll\Header.asm"
.include "\SrcAll\BasicMacros.asm"
.include "\SrcALL\AdvancedMacros.asm"
.include "\SrcALL\MemoryMap.asm"
 
CursorX equ 0x100 ;text drawing location X
CursorY equ 0x101 ;text drawing location Y
 
main:
jal Cls
nop
la a0,UnixDict ;pointer to start of the file
li t0,18 ;how many lines to read
@@loop:
jal PrintLine_UnixDict ;prints line to TV screen
nop
jal NewLine
;moves to new line - my print routine didn't actually use the CRLF to do this. Whoops!
nop
subiu t0,1
bnez t0,@@loop
nop
halt:
nop ;extra nop keeps project 64 from crashing
j halt ;loop forever because we're done.
nop
MyFont:
 
.ifdef buildn64
.incbin "\ResN64\ChibiAkumas.fnt"
.endif
.ifdef buildPSX
.incbin "\ResPSX\ChibiAkumas.fnt"
.endif
 
UnixDict:
.incbin "\unixdict.txt"
.byte 0 ;null terminator is used to mark the end of the file.
.align 4
PrintLine_UnixDict:
;same as PrintString but uses CR as terminator
push ra
pushSs ;these macros push multiple registers of the named "type"
pushTs
pushVs
PrintLine_UnixDict_DrawChars:
lbu a1,0(a0) ;Load a character
addiu a0,1 ;Move to next Char
li t0,13
beq t0,a1,PrintLine_UnixDict_Done ;Done?
nop
jal PrintChar ;Show Character A0 to the screen
nop
j PrintLine_UnixDict_DrawChars ;Repeat
nop
PrintLine_UnixDict_Done:
popVs ;;these macros pop multiple registers of the named "type"
popTs
popSs
pop ra
jr ra ;Return
addiu a0,1 ;Move to next Char
.include "\SrcALL\graphics.asm"
.include "\SrcAll\monitor.asm"
.include "\SrcALL\Multiplatform_Math_Integer.asm"
.include "\SrcALL\Multiplatform_BCD.asm"
.include "\SrcALL\BasicFunctions_v2.asm"
.include "\SrcN64\Footer.asm"</lang>
 
=={{header|Neko}}==
1,489

edits