[Gambas-user] How to write to a file??

charlesg charles at ...1784...
Thu Sep 18 09:50:57 CEST 2008


Hi Brittany,

You have 2 options: write to a simple text file or write to a database.

Writing to a text file is simple.

'-----------------------------------------
' Gambas class file
PUBLIC hFile AS File

PUBLIC SUB form_Activate()
  'create the file if it does not exist, otherwise just open it 
  IF Exist(Application.path & "/file.txt") THEN  
    hFile = OPEN Application.path & "/file.txt" FOR WRITE APPEND 
  ELSE 
    hFile = OPEN Application.path & "/file.txt" FOR WRITE CREATE
  ENDIF 
  TextBox1.SetFocus
END

PUBLIC SUB btnExit_Click()
  hFile.Close
  FMain.Close
END

PUBLIC SUB btnWrite_Click()
  'write the 2 textboxes to the File
  PRINT #hFile, TextBox1.text & "," & TextBox2.text
  'clear the textboxes and put the cursor back in the first
  TextBox1.text = ""
  TextBox2.text = ""
  TextBox1.SetFocus
END
'------------------------------

You can check the file (file.txt) by loading it in any text processor.
Because of the 'application.path', you will find it in the same directory as
your program.

Writing to a database is more flexible and a little more difficult. You can
create a simple SQLite database from Gambas though it is probably easier
using a very adequate add-on to Firefox (click Tools,add-ons and search
sqlite). You can see how to open and write to sqlite with a small tutorial
at www.kalaharix.wordpress.com.

rgds
-- 
View this message in context: http://www.nabble.com/How-to-write-to-a-file---tp19544483p19547768.html
Sent from the gambas-user mailing list archive at Nabble.com.





More information about the User mailing list