[Gambas-user] Newbie question

ron ronstk at ...239...
Thu Sep 16 03:45:53 CEST 2004


On Wednesday 15 September 2004 23:18, Valerio Guaglianone wrote:
> Hi,
> a newbie question:

Wellcome in the world of gambas.


> How do i read text file?
> 
> The following code is correct?

For a newbie not complete wrong or correct.


> PUBLIC SUB Button3_Click()
>     DIM hFile AS File
>     OPEN "error.txt" FOR READ AS #hFile
>     WHILE NOT Eof(hFile)

In opposite of VB or VBA you 'must' declare all 
variables you want to use with gambas.

>          LINE INPUT #hFile, OneLine
>          TextArea1.Text=PRINT OneLine

Where did you find you need PRINT OneLine ?
Take a look in help at GAMBAS.PRINT again. 
PRINT is sending the text in OneLine to the console.

>     WEND
>     CLOSE #hFile
> END
> 
> Thats it.
> 

Sofar so good.
But the file as one text or line by line?

> Thank :-)
>

PUBLIC SUB Button3_Click()
    DIM OneLine as String ' <----- Add this line, the declaration
    DIM hFile AS File

    OPEN "error.txt" FOR READ AS #hFile
    WHILE NOT Eof(hFile)
         LINE INPUT #hFile, OneLine
         TextArea1.Text= OneLine
        ' line by line
 WAIT 5.0 ' <------ wait 5 seconds between lines, see help GAMBAS.WAIT
    WEND
    CLOSE #hFile

END



If you want the whole text in one go, change
   TextArea1.Text= OneLine 
to
   TextArea1.Text= TextArea1.text & OneLine & "\n" ' < --- see gb.gb.NewLine
and do not use the WAIT


Or use the short method Rob posted here. :)
PUBLIC SUB Button3_Click()
   TextArea1.Text = File.Load("error.txt")
END





More information about the User mailing list