FileClose (int handle)Closes the file with handle HANDLE, and commits all changes to disk. You MUST close the file after you have finished with it.
Example:
int handle = FileOpen ("test.dat", FILE_WRITE);
FileWrite (handle, "test string");
FileClose (handle);
will open the file test.dat write the string "test string" and close it.See Also: FileOpen
FileIsEOF (int handle)Checks whether the specified file has had all its data read. This is only useful with files opened for reading. It returns 1 if the entire contents of the file has now been read, or 0 if not.
Example:
int handle = FileOpen ("test.dat", FILE_READ);
while (FileIsEOF(handle) == 0) {
int temp = FileReadRawChar (handle);
Display("%c", temp);
}
FileClose (handle);
will display every character in the file test.dat, one by one, to the screen.See Also: FileIsError, FileOpen, FileRead
FileIsError(int handle)Checks whether an error has occured reading from or writing to the specified file.
An error can occur if, for example, you run out of disk space or the user removes the disk that is being read from.
This function only checks for errors while actually reading/writing data. The FileOpen function will return 0 if there was an error actually opening or creating the file.
To find out whether all data has been read from a file, use FileIsEOF instead.
Example:
int handle = FileOpen("test.dat", FILE_WRITE);
FileWriteInt(handle, 51);
if (FileIsError(handle)) {
Display("Error writing the data!");
}
FileClose (handle);
will write a number to the file 'test.dat', and display a message if there was a problem.
FileOpen (string filename, int mode)Opens a disk file for reading or writing. These disk I/O functions are only intended for simple tasks like the way the QFG series export the character when you complete it.
MODE is either FILE_READ, FILE_WRITE or FILE_APPEND, depending on whether you want to write to or read from the file. If you pass FILE_WRITE and a file called FILENAME already exists, it will be overwritten.
FILE_APPEND opens an existing file for writing and starts adding information at the end (ie. the existing contents are not deleted).
This function returns a file handle, which you use in future calls to file functions, or returns 0 if there was a problem (eg. file not existing when MODE is FILE_READ).
NOTE: You MUST close the file with FileClose when you have finished using it. There are only a limited number of file handles, and forgetting to close the file can lead to problems later on.
IMPORTANT: If you open the file for writing, then you can ONLY work with files in the game directory. You CANNOT use a path, so any filename with "\" or "/" in it will automatically be rejected, for security reasons.
Example:
int handle = FileOpen ("temp.tmp", FILE_WRITE);
if (handle == 0) Display("Error opening file.");
else {
FileWrite (handle, "test string");
FileClose (handle);
}
will open the file temp.tmp for writing. An error message is displayed if the file could not be created.
Otherwise, it will write the string "test string" to the file and close it.See Also: FileClose, FileRead, FileWrite
FileRead (int handle, string buffer)Reads a string into BUFFER, from a file previously opened with FileOpen which returned HANDLE. You should only use this with files which you previously wrote out with FileWrite. Do NOT use this function with any other files, even text files.
Example:
string buffer;
int handle = FileOpen ("test.dat", FILE_READ);
FileRead (handle, buffer);
FileClose (handle);
will open the file test.dat (which you have previously written with FileWrite) and read a string into the buffer. Then close the file.
FileReadInt (int handle)Reads an integer from the file HANDLE, and returns it to the script. Only integers written with FileWriteInt can be read back.
Example:
int number;
int handle = FileOpen ("stats.dat", FILE_READ);
number = FileReadInt (handle);
FileClose (handle);
will open the file stats.dat read an integer pass it to number and then close the file.See Also: FileRead, FileWriteInt
FileReadRawChar (int handle)Reads a raw character from the input file HANDLE and returns it. This function allows you to read from files that weren't created by your game, however it is recommended for expert users only.
Example:
string input;
int handle = FileOpen ("stats.txt", FILE_READ);
StrFormat (input, "%c", FileReadRawChar(handle));
FileClose (handle);
will read a raw character from file stats.txt and writes it to the string 'input'.See Also: FileRead, FileReadRawInt, FileWriteRawChar
FileReadRawInt (int handle)Reads a raw 32-bit integer from the input file and returns it to the script. This allows you to read from files created by other programs - however, it should only be used by experts as no error-checking is performed.
Example:
int number;
int handle = FileOpen ("stats.txt", FILE_READ);
number = FileReadRawInt(handle);
FileClose (handle);
will read a raw integer from file stats.txt and put it into the integer number.See Also: FileRead, FileReadRawChar
FileWrite (int handle, string text)Writes TEXT to the file HANDLE, which must have been previously opened with FileOpen for writing. The string is written using a custom format to the file, which can only be read back by using FileRead.
Example:
int handle = FileOpen ("temp.tmp", FILE_WRITE);
if (handle == 0) Display("Error opening file.");
else {
FileWrite (handle, "test string");
FileClose (handle);
}
will open the file temp.tmp for writing. If there’s no such file it will display an error message. If the file exists it will write the string "test string" and close it.See Also: FileRead, FileOpen, FileWriteRawLine
FileWriteInt (int handle, int value)Writes VALUE to the file HANDLE. This allows you to save the contents of variables to disk. The file must have been previously opened with FileOpen, and you can read the value back later with FileReadInt.
Example:
int number;
int handle = FileOpen ("stats.dat", FILE_WRITE);
FileWriteInt (handle,number);
FileClose (handle);
will open the file stats.dat and write the integer number in it.See Also: FileReadInt, FileWrite
FileWriteRawChar(int handle, int value)Writes a single character to the specified file, in raw mode so that other applications can read it back. If you are just creating a file for your game to read back in, use FileWriteInt instead because it offers additional protection. Only use this function if you need other applications to be able to read the file in.
This command writes a single byte to the output file - therefore, VALUE can contain any value from 0 to 255.
Example:
int handle = FileOpen("output.txt", FILE_WRITE);
FileWriteRawChar(handle, 'A');
FileWriteRawChar(handle, 'B');
FileWriteRawChar(handle, 13);
FileClose(handle);
will write the text "AB", followed by a carriage return character, to the file.See Also: FileReadRawChar, FileWriteInt
FileWriteRawLine (int handle, string text)Writes a string of text to the file in plain text format. This enables you to read it back in Notepad or any editor; however, your game cannot read the file back in. This is useful for generating logs and such like. The TEXT will be printed to the file, followed by the newline characters.
Example:
int handle = FileOpen ("error.log", FILE_WRITE);
FileWriteRawLine (handle,"There was an error playing sound1.wav");
FileClose (handle);
will write an error line in the file error.log.See Also: FileWrite