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
im sure you defined
MyFile
but you used
myfile
i think its the problem
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
wow, EB is so easy! :D
in other languages variables are case sensitive
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 (http://www.ionicwind.com/forums/index.php/topic,3075.0.html) for complete discussion and for the answer to the question.
Barney
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 (http://www.ionicwind.com/forums/index.php/topic,3075.0.html) 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
did you globalise the HTMLDOCUMENT variable?
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
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
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