format(string, 30, "şьğes\r\n"); file = fopen("hareket.txt", io_append); fwrite(file, string);
This function writes a zero-terminated buffer, presumably a text string, to the file. If the text string is in unpacked format, it is written to the file in UTF-8 encoding. A packed string is written to the file “as is”. |
Character encoding. The function writes to the file in UTF-8, which does not support these special characters. Try packing the string (strpack).
|
new string[128 char]; strpack(string, "şьğes"); new File:file; file = fopen("hareket.txt", io_append); fwrite(file, string); fclose(file);
strpack Create a “packed” copy of a string Syntax: strpack(dest[], const source[], maxlength=sizeof dest) dest The buffer to store the packed string in. source The string to copy, this may be a packed or an unpacked string. maxlength If the length of dest would exceed maxlength cells, the result is truncated to maxlength cells. Note that several packed characters fit in each cell. Returns: The number of characters copied. Notes: This function copies a string from source to dest where the destination string will be in packed format. The source string may either be a packed or an unpacked string. |
native fwritechar(File: handle, value, bool: utf8 = false) = fputchar;
native freadchar(File: handle, value = 0, bool: utf8 = false) = fgetchar;
stock fwriteEx(File:handle, string[]) {
if(0 <= string[0] <= ucharmax) { // ispacked(string) doesn't seem to work with these characters
for(new i; string[i]; ++i) {
fwritechar(handle, string[i]);
}
} else {
for(new i; string{i}; ++i) {
fwritechar(handle, string{i});
}
}
}
stock freadEx(File:handle, string[], size = sizeof string, bool: pack = false) {
if(pack) {
size *= 4;
for(new i; i < size; ++i) {
if((string{i} = freadchar(handle)) == EOF) {
string{i} = EOS;
break;
}
}
} else {
for(new i; i < size; ++i) {
if((string[i] = freadchar(handle)) == EOF) {
string[i] = EOS;
break;
}
}
}
}
new
dest[16],
string[] = !"şьğes", // fwriteEx supports packed and unpacked string
filename[] = "hareket.txt"
;
new File: wFile = fopen(filename, io_write);
if(wFile) {
fwriteEx(wFile, string);
fclose(wFile);
print(string); // print supports both packed and unpacked string instead of printf
}
new File: rFile = fopen(filename, io_read);
if(rFile) {
freadEx(rFile, dest); // freadEx(rFile, dest, .pack = true) to read it as a packed string
fclose(rFile);
print(dest);
}