Post by 8XenoTrogdor8 on Dec 24, 2004 22:01:48 GMT -5
Here's a little example of how to write to and read from a .txt file. This demo uses the test.txt file that comes with your Basic4GL program so you don't have to make your own!
' How to write to and read from files-
' First, make a variable to store the file directory.
Dim File
' For Writing-
' Use OpenFileWrite to open the file from the directory.
File = OpenFileWrite ("files/test.txt")
' Now the file is ready to be written to. There are several file writing commands
' listed in the "programmer's guide. The following are ones I find the most useful--
' WRITELINE - writes a text string. This is similar to using "Printr".
' (it goes to a new line after writing the text.)
WriteLine (File, "Hello")
' WRITESTRING - writes a text string. This is similar to using "Print".
' (it stays on the same line after writing the text.)
WriteString (File, "World")
' If you don't close the file, it might not write everything to it.
CloseFile (File)
' For Reading-
' Use OpenFileRead to open the file and read from it.
File = OpenFileRead ("files/test.txt")
' Now the file is ready to be read. As with file writing, there are several ways to
' read from a file listed in the "programmer's guide". Here's a couple big ones--
' (We can see the result of each file-read by using "print" or "printr".)
' READLINE reads the entire line and returns it as a text string.
' (after it is done, it sets the read position on the next line.)
Printr ReadLine (File)
' READTEXT skips over all white space until it finds some text. It will return the
' text as a text string until it hits white space again. The True/False value in (File, True)
' determines whether it will stop at the end of a line or not. (if true, it will disregard the
' end of a line, if false it will stop at the end of a line.)
Printr ReadText (file, True)
CloseFile (File)
' Another Useful Command-
' ENDOFFILE will iether return true (if at the end of a file) or false (if else).
' This can be used to print the entire file to look exactly like it does in the document.
File = OpenFileRead ("files/test.txt")
Printr "Printing entire file :"
PrintNextLine:
Printr ReadLine (File)
if not EndOfFile (File) then
goto PrintNextLine
endif
CloseFile (File)
Print ": End of file"