I am trying to write a Karel program that will read all existing lines in a .dt file.
Currently my .dt file has 3 lines, simply:
And I want to read all 3 lines with KAREL, this is my KAREL code:
Code
PROGRAM ReadingFromFile
CONST
RW = 'RW'
RO = 'RO'
FILENAME = 'TEST.dt'
VAR
testFile: FILE
status: INTEGER
tempString: STRING[126]
fileName: STRING[126]
BEGIN
OPEN FILE testFile(RO, FILENAME)
REPEAT
READ testFile(tempString, CR)
WRITE(tempString, CR)
status = IO_STATUS(testFile)
UNTIL status <> 0
END ReadingFromFile
Display More
It is almost working, but it is reading only the first line, when it attempts to read the second line I get the error:
Line 19 is the line with the WRITE statement, so on the second iteration of the loop, no data was read and tempString is empty, (im assuming), why is that?
What is the correct way to read all lines from a file?