Pages

6. A Fundamental Set of Instructions Part 3

A Fundamental Set of Instructions Part 3

Recap

In the previous post we continued with Fundamental and demonstrated integer variables, string concatenation and functions.  In this post we will work with text files.  The ability to store information on your hard disk between runs of a program is quite powerful.  Some of the things you can store are:

  • a program's configuration settings;
  • your game's high score table;
  • your progress in a game;
  • data that you need to save for later printing or to use in an external program.
To end our recap, we have completed lines 1 through 5 of the listing below, including the function.

Fundamental
  1. ask “what is your name?”
  2. say “hello” name
  3. ask “when were you born” name “?”
  4. age is getAge(date)
  5. say “Hi name, at the end of 2017 you will be “ age “ years old”
  6. write name, dob, age to text file
  7. clear screen
  8. reset name, dob, age
  9. read name, dob, age from text file
  10. print name “was born on” dob “and so is “ age “ years old”
function getAge (date)
  1. calculate age from date
  2. return age

Writing to a Text File

Let's get our program writing to a text file. FreeBASIC uses the idea of file numbers to refer to files, and uses the FreeFile function to return the next free file number.  This number is in the range of 1 to 255, meaning that you can have a maximum of 255 files open.  Why would you want to do that?  I've got no idea!  After 255, if you try to open one more file you will get a 0 returned, meaning that you have hit your limit (you should get some professional help).

The actual code starts of by creating a variable to hold the file number.  In the Dim section of Fundamental enter the following:

Dim As Integer myFile = 0

An integer is a suitable data type to hold any file number.

Below the section of code that displayed our age in the previous post enter the following:
myFile = FreeFile()
Open "test.txt" for Output as #myFile

In the first line we are setting the new variable to the result of the FreeFile() built in function.  Notice the function FreeFile does not take parameters, unlike getAge() that required a year.  So long as the block of code returns data it is considered a function.

The second line opens a file called test.txt in a mode that allows it to accept data.  The as #myFile specifies the free file number we picked up in the previous line. A file should be opened as close as possible to the time you get the free file number.  Getting a series of free file numbers and then trying to open them in a later part of the program can lead to confusing results.  If test.txt does not exist in the default directory, in this case the directory that holds fundamental.bas, then it is created.  If it does exist the for Output overwrites the data that is already in the file.

The next four lines will write data to the file and finally close the file.  It is important to close files, especially if you use many files in your program:
print #myFile, yourName
print #myFile, Str(yourDob)
print #myFile, Str(getAge(yourDob))
close #myFile

My program looks like the following:
Fundamental writing to a file
If you compile and run the program you should find the new file, test.txt, in your directory.  If you open it with a text editor it will contain the three bits of data you entered during the run.

Let's look at reading the file now.  First off  let's do like magicians and show we have nothing up our sleeves.  Enter the following after the close #myFile line:

cls
yourName = ""
yourDob = 0

The cls command clears the screen and the next two commands clear out the variables we are using.

We will need to create two new string variables called yourFileAge and yourFileDob to hold the age and the dob read from the file.  Go ahead, you should know how to do this.

To read from a file we need to open it for Input then read each line.  Don't forget to close the file when we are done.  Enter the following:

Open "test.txt" for Input as #myFile
Line Input #myFile, yourName
Line Input #myFile, yourFileDob
Line Input #myFile, yourFileAge
Close #myFile

Let's display the result:

Print yourName + " was born on " + yourFileDob + " and is " + yourFileAge + " years old."

Your program should now look like the following (I won't show the function as that hasn't changed).  Notice I have split the print line over two lines with an underscore character.

Fundamental with file read

Go ahead and run the program.

That's it for the program I described a few posts back.  Fundamental is a simple program but it packs quite a few basic programming ideas ideas.  In future posts we will look at other features of FreeBASIC that will round out methods that can be used in most programs.  We will look at arrays to hold related data, loops to repeat sections of code, and commands that control which bits of code will run.  If there is anything in particular you wish to see covered please leave a comment.
Please feel free to leave a comment and see you in the next post.

2 comments:

  1. I have a FreeBASIC program which writes just over 100,000 integers to a file. I would like to see an example of how to read such a file.

    ReplyDelete
  2. Hi Dale - I will put up a post over the next couple of days that creates a file with 100,000 integers then reads the file back into an array, allow us to work with the values.

    ReplyDelete