IonicWind Software

IWBasic => General Questions => Topic started by: TexasPete on January 19, 2009, 05:59:49 PM

Title: Reading a simple text file
Post by: TexasPete on January 19, 2009, 05:59:49 PM
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
Title: Re: Reading a simple text file
Post by: yujinwunz on January 19, 2009, 06:11:03 PM
im sure you defined

MyFile

but you used

myfile

i think its the problem
Title: Re: Reading a simple text file
Post by: LarryMc on January 19, 2009, 06:55:03 PM
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
Title: Re: Reading a simple text file
Post by: yujinwunz on January 19, 2009, 07:44:24 PM
wow, EB is so easy!  :D

in other languages  variables are case sensitive
Title: Re: Reading a simple text file
Post by: 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
Title: Re: Reading a simple text file
Post by: yujinwunz on January 19, 2009, 08:39:47 PM
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
Title: Re: Reading a simple text file
Post by: yujinwunz on January 19, 2009, 08:48:31 PM
did you globalise the HTMLDOCUMENT variable?
Title: Re: Reading a simple text file
Post by: fasecero on January 20, 2009, 02:44:12 AM
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

Title: Re: Reading a simple text file
Post by: billhsln on January 20, 2009, 06:36:39 AM
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
Title: Re: Reading a simple text file
Post by: TexasPete on January 22, 2009, 05:31:42 AM
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