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.
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).
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.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.
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 pass it into the string char.See Also: FileRead, FileReadRawInt
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
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