#Trouble writing to file. Wrong encoding?

18 messages · Page 1 of 1 (latest)

burnt mica
#

I had previously set the locale with setlocale(LC_ALL, ""); I reset the locale to the C before trying to write to a file but opening the file it puts it in binary mode. I suspect this is due to wrong encoding.

    setlocale(LC_ALL, "C");

    FILE *file = fopen("actions.txt", "w");
    for(int i = 0; i < arrlen(game.action_history); ++i) {
        struct Action *action = &game.action_history[i];
        char buffer[32];
        char color = PIECE_COLORS[action->piece];
        char species = LETTER[PIECE_SPECIES[action->piece]];
        int number = PIECE_NUMBERS[action->piece];
        snprintf(buffer, 32, 
            "%c%c%d %d %d %d %d %d %d\n", 
            color, species, number,
            action->source.q, action->source.r, action->source.s,
            action->destination.q, action->destination.r, action->destination.s);
        fwrite(buffer, 32, 1, file);
    }
    fclose(file);
raw currentBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

burnt mica
#

hmm

frail tangle
#

Are you writing non-ascii stuff to the file?

#

What encoding do you want the file to have?

burnt mica
#

I am just writing ascii characters like WS1 0 0 0 0 0 0 -1

#

oh i think i called fwrite incorrectly

#

Sorry false alarm i guess.

frail tangle
#

I wouldn't mess with locales at all.

burnt mica
#

I am displaying unicode characters to the terminal

#

as well

#

I don't know much about locales.

#

If i don't set the locale to utf-8 it doesnt print the unicode characters

#

I probably did something wrong

#

trying to debug it now

#

The issue was i was writing bytes past the count of bytes written to the buffer

#

Just needed to get the count from snprintf

#

!solved