Sample code for creating and accessing data files

I'm conversant in Liberty Basic and am just starting to learn True Basic. I'm lost as to how to create and then access a simple data file. I've read the manuals but can't make head nor tails out of them. Can someone just shoot me a sample of simple code that creates a file, puts data in the file, and then accesses it. Please keep it simple. I'm about to give up.




sample code ... files


First, in True basic there are different types of files, but the simplest to get started with is a plain "text" file. Anything can be stored in a text file; it is just some bunch of ascii characters which have some meaning depending upon what it pertains to. Could be an email text, an array of numerical data, an address book; data for an Excel file, even a True Basic program; just about anything. I'm sure you already know this.

Here is a very short program to save a single line of text in a ".txt" file, then open it and print the contents:

! ==================================================
LET Line$="line of text to be saved in a text file"

LET filename$="Saved text-file.txt" ! this will be the file name to save under.

! You can prefix the complete path as well, such as: "C:\windows\temp\Saved text-file.txt"
! Select some channel number that's not otherwise in use, say, 99.

CLOSE #99 ! close the channel in case it was already open
OPEN #99: NAME filename$, ORG "text", CREATE newold

! ORG tells TB it is to be a text file; newold means it will open an old file with that same name if present, or create a new one.
ERASE #99 ! This will erase the contents of the file if it already exists.
PRINT #99: Line$

print ! this part is just "bells & whistles" --------------
SOUND 800,.2
set cursor 3,1
print, "The data: """;
set color 12
print line$;
set color 0
print """ has been saved in a text file named """ & filename$ & """. Press any key to continue."
get key zz ! ----------------------------------------

! --- Now to OPEN THE FILE and retrieve its contents:

CLOSE #99 ! close the channel in case it was already open
OPEN #99: NAME filename$, ORG "text", CREATE newold
LINE INPUT #99: SavedLine$

! ------------------------- more "bells & whistles" --------------
SOUND 600,.18
print, "The file named """ & filename$ & """ has been opened and contains the data:"
set color 12
print, SavedLine$
set color 0
print,"Press any key to conclude"

END ! =====================================

Beyond this, things can get considerably more involved depending upon what you're trying to do... Perhaps the newer books and manuals have better descriptions of dealing with files, but the old ones like I have are rather scatterbrained.

Note that True Basic is not case sensitive, so CAPs are not really needed except that they can sometimes help with clarity. Also if you place the command OPTION NOLET at the beginning of the program, you don't need the pesky "LET" in front of every assignment statement.

At any rate, if you have any questions or need further help with anything specific, email me. This forum doesn't lend itself to lengthy explanations.

Hope this helps
Mike C.

get the primary docs and view the samples online

Let's Program It chp 9
Bronze Manual chp 12

File I/O in BASIC is about as simple as it gets honestly;


Your program opens the channel, writes to it (usually with a PRINT statement) and then closes the channel. (the details vary depending whether you want the file for text or binary, sequential vs random access, etc)

Both docs referenced above have very simple clear-cut example codes that are straight-forward and very easy to copy and understand. Also, there are examples online from "Big John's" book too:

World Tour of True BASIC for Programming Windows: see chp 18 File Handling