Foreign Character Problem with Fwrite
#1

Hello guys.
I'm writing a small filterscript for my movie server, it logs the actions I conduct for future use. The problem is, foreign characters are not properly transferred into the log files.
Код:
format(string, 30, "şьğes\r\n");
file = fopen("hareket.txt", io_append);
fwrite(file, string);
"şьğes" turns out as "Юьрes" in hareket.txt, how can I solve this?
Reply
#2

Character encoding. The function writes to the file in UTF-8, which does not support these special characters. Try packing the string (strpack).

Quote:

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”.

Reply
#3

Quote:
Originally Posted by Vince
Посмотреть сообщение
Character encoding. The function writes to the file in UTF-8, which does not support these special characters. Try packing the string (strpack).
Thanks for your answer.

I've been searching the forums about strpack since you've posted, hoping to find some instructions, but I couldn't find much. Pawn PDF doesn't tell how it is used either. It appears to be a not-so-popular feature, or I am searching it the wrong way, not sure. I tried the following;

Код:
new string[128 char];
strpack(string, "şьğes");
new File:file;
file = fopen("hareket.txt", io_append);
fwrite(file, string);
fclose(file);
Result was "Þüðüöç". What am I doing wrong?

EDIT:
Okay I found the related article in a PDF. Didn't know there were multiple official PDF files.
Quote:

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.

EDIT 2:
Well, finding the PDF didn't help, I still can't get the script to write in an intelligible manner : (
Reply
#4

Anyone?
Reply
#5

I still need help with this one
Reply
#6

Bump
Reply
#7

Well I just found out foreign characters are printed just fine in server_log.txt, but fwrite doesn't tolerate them

still need help guys
Reply
#8

Bampf
Reply
#9

It is pretty frustrating to see that no one in this whole community has the slightest opinion or cares to make an input
Reply
#10

You need to use fputchar / fgetchar with extended ascii if uft8 doesn't work

Now create some functions to make the live easier
pawn Код:
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;
            }
        }
    }
}
And your code
pawn Код:
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);
}
Well it prints twice the same thing and it is saved correctly in the file, therefor it works
But for me it still looks different in the console, probably because it doesn't support these charaters
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)