May 09, 2024, 05:22:45 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Text file example reading problem

Started by TexasPete, January 11, 2009, 08:10:53 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

TexasPete

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

LarryMc

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
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

TexasPete