May 20, 2024, 04:32:40 AM

News:

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


Reading a simple text file

Started by TexasPete, January 19, 2009, 05:59:49 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

TexasPete

I am trying to read a text file into the array. For some reason the code is not detecting eof. I am building off of the example in the book.
SUB READTEXTFILE
DEF MyFile as FILE
DEF Textline:STRING

'-------Empty Html Document the Array-------------
for X= 1 TO Lines :HTMLDOCUMENT[X]="":NEXT X
Lines =1
IF(OPENFILE(myfile,READHTMLFILE,"R") = 0)
WHILE EOF(myfile) = 0
            IF READ(myfile, Textline) = 0
                'do something with the data
               HTMLDOCUMENT[lines]=Textline
            MESSAGEBOX MAIN_WINDOW,Textline,"Line of text",@MB_OK
         ENDIF
          Lines=Lines+1
    ENDWHILE
    CLOSEFILE myfile
ENDIF
return
ENDSUB

if there is some one who can see my error . I would appreciate it.

Thanks Texas Pete

yujinwunz

im sure you defined

MyFile

but you used

myfile

i think its the problem

LarryMc

Quote from: yujinwunz on January 19, 2009, 06:11:03 PM
im sure you defined

MyFile

but you used

myfile

i think its the problem
that makes no difference

Larry
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

yujinwunz

wow, EB is so easy!  :D

in other languages  variables are case sensitive

Barney

As Larry pointed out it's not the variables but the program itself that is wrong. What I can't understand is why TexasPete is asking the same question he already asked only a few days ago. See here for complete discussion and for the answer to the question.

Barney

yujinwunz

Quote from: Barney on January 19, 2009, 08:26:58 PM
As Larry pointed out it's not the variables but the program itself that is wrong. What I can't understand is why TexasPete is asking the same question he already asked only a few days ago. See here for complete discussion and for the answer to the question.

Barney

on the other post he did not have a loop, but this time he uses the while loop. it's not the same question

yujinwunz

did you globalise the HTMLDOCUMENT variable?

fasecero

It's rare, i tried your function (a little more generic) and works fine, don't know what's happening, but maybe this help



string HTMLDOCUMENT[5]

OPENCONSOLE
' FILL THE ARRAY
HTMLDOCUMENT[0]="hi"
HTMLDOCUMENT[1]="what's up"
HTMLDOCUMENT[2]="how are you"
HTMLDOCUMENT[3]="i'm very well"
HTMLDOCUMENT[4]="thank you"
' WRITTING TO A FILE
WRITETEXTFILE("doc.txt", HTMLDOCUMENT, 5)
' READ THE FILE
READTEXTFILE("doc.txt", HTMLDOCUMENT, 5)
DO:UNTIL INKEY$ <> ""
CLOSECONSOLE
END

' THIS FUNCTION WRITES AN ARRAY OF STRINGS TO A FILE
SUB WRITETEXTFILE(filename AS STRING, array[] AS STRING, linesnumber AS INT)
DEF MyFile as FILE
DEF Textline:STRING

' OPENING THE FILE
IF OPENFILE(MyFile, GETSTARTPATH+filename, "W")=0 THEN
' WRITTING TO THE FILE
FOR X= 0 TO linesnumber-1
WRITE(MyFile, array[X])
NEXT X
' SHOW THE NUMBER OF LINES WRITTEN
PRINT STR$(linesnumber)+" LINES WRITTEN"
PRINT ""
' CLOSE THE FILE
CLOSEFILE(MyFile)
ENDIF
ENDSUB

SUB READTEXTFILE(filename AS STRING, array[] AS STRING, linesnumber AS INT)
DEF MyFile as FILE
DEF Textline:STRING

'-------Empty Html Document the Array-------------
for X= 1 TO linesnumber :array[X-1]="":NEXT X

PRINT "READING FROM FILE"
Lines=1
IF(OPENFILE(myfile,filename,"R") = 0)
WHILE EOF(myfile) = 0
IF READ(myfile, Textline) = 0
'do something with the data
array[Lines-1]=Textline
PRINT "text "+str$(Lines)+" : "+array[Lines-1]
ENDIF
Lines=Lines+1
ENDWHILE
CLOSEFILE myfile
ENDIF
ENDSUB


billhsln

Fixing your original code:

SUB READTEXTFILE
DEF MyFile as FILE
DEF Textline:STRING

'-------Empty Html Document the Array-------------
for X= 1 TO Lines :HTMLDOCUMENT[X]="":NEXT X
Lines =1
IF(OPENFILE(myfile,READHTMLFILE,"R") = 0)
WHILE (READ(myfile, Textline) = 0)
         'do something with the data
          HTMLDOCUMENT[lines]=Textline
          MESSAGEBOX MAIN_WINDOW,Textline,"Line of text",@MB_OK
          Lines=Lines+1
    ENDWHILE
    CLOSEFILE myfile
ENDIF
return
ENDSUB


This works.

Bill
When all else fails, get a bigger hammer.

TexasPete

Thanks for all the help. I am getting over throat surgery and been on a lot demiral this last week. I have not been able to program much just a little every once in a while . I have written text file reading sub routines is several basic languages.
Any way I am sorry I posted twice.
Texas Pete