Writing to a file

Description

There will likely be occasions when your game requires the ability to store data on file, this is where the file access commands in AGK come into play. Through the use of these commands it's possible to store data and read this back in.

How to open a file and write data to it is demonstrated in this document.

Overview

The example program takes the following steps:

Open a file for writing

The command to open a file for writing is called OpenToWrite. It either takes two or three parameters dependant on usage. The first option is to have the command return an ID number for later usage, when taking this option the command only requires two parameters - the filename and whether you want to append to it. If you want to specify an ID number then the command takes three parameters - the ID, the filename and whether you want to append to the file.

This line of code shows the first option:

fileID = OpenToWrite ( "myfile.txt", 0 )

An ID for the file is returned to the variable fileID, the first parameter "myFile.txt" tells AGK that we want to open this particular file, while the final parameter tells AGK that we do not want to append to the file, therefore this will be a completely new and empty file. If this value was 1 anything that we later wrote to the file would be appended to the end.

The alternative approach is to specify an ID number manually, instead of it being assigned in the return value:

OpenToWrite ( 1, "myfile.txt", 0 )

For the purpose of our example an ID number will be specified manually.

Writing data to a file

There are three possible data types that can be written to a file:

Writing an integer to file

To write an integer to file call the WriteInteger command. This command takes two parameters - ID of the file and an integer. This line of code writes the value 10 to file 1

WriteInteger ( 1, 10 )

Here's another example where 3 integer variables are written to file:

a = 54
b = 99
c = 12

WriteInteger ( 1, a ) WriteInteger ( 1, b ) WriteInteger ( 1, c )

Writing a string to file

To write a string to file call the WriteString command. This command takes two parameters - ID of the file and a string. This line of code writes the string "hello" to file 1

WriteString ( 1, "hello" )

Here's another example where 3 strings are written to file:

a$ = "abc"
b$ = "def"
c$ = "ghi"

WriteString ( 1, a$ ) WriteString ( 1, b$ ) WriteString ( 1, c$ )

Writing a float to file

To write a float to file call the WriteFloat command. This command takes two parameters - ID of the file and a float. This line of code writes the value 1.23 to file 1

WriteFloat ( 1, 1.23 )

Here's another example where 3 floating point variables are written to file:

a# = 1.85
b# = 22.39
c# = 4489.0012

WriteFloat ( 1, a# ) WriteFloat ( 1, b# ) WriteFloat ( 1, c# )

Closing a file

Once all data has been written to a file that file must be closed using the command CloseFile. This commands only parameter is the ID of the file. To close file 1:

CloseFile ( 1 )

Full code listing

Our final program will open a file for writing and write an integer, a float and a string:

OpenToWrite ( 1, "myfile.txt", 0 )
WriteInteger ( 1, 10 )
WriteFloat ( 1, 1.23 )
WriteString ( 1, "hello" )
CloseFile ( 1 )

Conclusion

As you can see from the above code, opening a file and writing data to it is a fairly simple process.