void FileWriteString(int file, string s)
Pass a handle to a file that is opened in write mode, and a string to be written. The string is written from the current position in the file, overwriting any existing data. It uses a 1 byte ascii representation for each character, and writes a 2 byte string length before the start of the string - so bytes used is equal to len(s) + 2. By storing the length first like this, it is possible to use FileReadString() to read a string of abritary length back.
int file = FileOpen("datafile.txt", FILE_WRITE);
FileWriteString(file, "a");
FileClose(file);
// This creates/overwrites a file three bytes in size
// Opened in a hex editor the contents will be: '01 00 61'
// Which is the ascii code of a little a
// With two bytes before saying the string is length one
// Note the least significant bit comes first
int file = FileOpen("datafile.txt", FILE_WRITE);
FileWriteString(file, "A string here");
FileWriteString(file, "And another one");
FileClose(file);
// Because the length is stored before the characters
// You can write strings however, and still read back
// As distinct separate valuesTalkback #1 written by Zip on 2004-09-30.
Because the length is stored in 2 bytes, the longest string you can write, and read back after, is 65535 characters. I have the 16 meg text file to prove this.
void AutoExec()
{
string dog = "a";
while (len(dog) < pow(2, 24))
{dog = strdup(dog, 2);}
//Add this and it works//dog = left(dog, len(dog) - 1);
int file = FileOpen("datafile.txt", FILE_WRITE);
FileWriteString(file, dog);
FileClose(file);
file = FileOpen("datafile.txt", FILE_READ);
dog = FileReadString(file);
Log(str(len(s)));
FileClose(file);
}
Doc Nav |
![]() |
Your docs |
![]() |