IonicWind Software

IWBasic => General Questions => Topic started by: TexasPete on January 11, 2009, 08:10:53 AM

Title: Text file example reading problem
Post by: TexasPete on January 11, 2009, 08:10:53 AM
This is and example of the text file reading example right out of the manual.
The only thing that I changed was the text file name. It does not work.
It read only one line of text and closed.
From what I can tell it is check for and end of file.
Can any one see the error !

OPENCONSOLE
DEF myfile:FILE
DEF ln:STRING
IF(OPENFILE(myfile,"test.TXT","R") = 0)
   IF(READ(myfile,ln) = 0)
            PRINT ln
   ENDIF
   CLOSEFILE myfile
   PRINT "File read successfully"
ELSE
   PRINT "File could not be opened"
ENDIF
PRINT "Press Any Key To Close"
DO:UNTIL INKEY$ <> ""
CLOSECONSOLE
END

Thanks
Texas Pete
Title: Re: Text file example reading problem
Post by: LarryMc on January 11, 2009, 09:07:42 AM
The program did exactly what it was programmed to do.
If you had moved down to the very next example you would have seen the example that reads until it hits the end of file marker.
If you want to read multiple lines you have to have a loop.

I know you said you were use to more documentation.
More documentation isn't much good unless you actually read it.

The READ command:
QuoteReturn value
Returns 0 on success or -1 if the file could not be read.
so this IF(READ(myfile,ln) = 0)
reads a line from the file and if it was successful it prints it with the next line.

Changing a section of the code inserts a WHILE-ENDWHILE loop that
reads and prints the lines untilk the end of the file is reached.
while EOF(myfile)=0
IF(READ(myfile,ln) = 0)
            PRINT ln
ENDIF
endwhile


It's in the documentation. ;)

Larry
Title: Re: Text file example reading problem
Post by: TexasPete on January 11, 2009, 02:29:09 PM
Thanks a lot larry,