# Reading from a file

## Description

Games often need to store data on a file, for example, high scores or level progress. The file access commands in AGK provide the ability to deal with tasks such as this. In this particular example commands are used that show the opening and reading of data from a file.

## Overview

The process involved is as follows:

- open a file for reading
- read data such as an integer, float and string
- close the file

## Open a file for reading

The command to open a file for writing is called OpenToWrite. It either takes one or two 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 one parameter - the filename. If you want to specify an ID number then the command takes two parameters - the ID and the filename.

This line of code shows the first option:

    fileID = OpenToRead ( "myfile.txt" )

    fileID = agk::OpenToRead ( "myfile.txt" );

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.

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

    OpenToRead ( 1, "myfile.txt" )

    agk::OpenToRead ( 1, "myfile.txt" );

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

## Reading data from a file

There are three possible data types that can be read from a file:

- integers
- strings
- floats

## Test file

Our test file will contain the following data:

- 10
- 1.23
- hello

## Read an integer from a file

To read an integer from file call the ReadInteger command. This command takes one parameter - the ID of the file. This line of code reads an integer from the file, in this case 10:

    value = ReadInteger ( 1 )

    value = agk::ReadInteger ( 1 );

## Reading a string from file

To read a string from a file call the ReadString command. This command takes one parameter - the ID of the file. This line of code read the string "hello" from file 1:

    string$ = ReadString ( 1 )

    string = agk::ReadString ( 1 );

## Reading a float from file

To read a float from a file call the ReadFloat command. This command takes one parameter - the ID of the file. This line of code reads the value 1.23 from file 1:

    value# = ReadFloat ( 1 )

    value = agk::ReadFloat ( 1 );

## Closing a file

Once all data has been read from 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 )

    agk::CloseFile ( 1 );

## Full code listing

Our final program will read from a file containing this data:

- 10
- 1.23
- hello

Here's the code:

    OpenToRead ( 1, "myfile.txt" )

    a = ReadInteger ( 1 )
    b# = ReadFloat ( 1 )
    c$ = ReadString ( 1 )

    CloseFile ( 1 )

    agk::OpenToRead ( 1, "myfile.txt" );

    a = agk::ReadInteger ( 1 );
    b = agk::ReadFloat ( 1 );
    c = agk::ReadString ( 1 );

    agk::CloseFile ( 1 );

## Conclusion

As you can see from the above code, opening a file and reading data from it is a simple process. The important point to note is that you must read data in the correct order. Our file had an integer, a float and then a string, so we had to read it in that order.