D
D
Danil Tunev2016-08-10 18:52:08
linux
Danil Tunev, 2016-08-10 18:52:08

C program. Problem writing multibyte character to file! gcc compiler?

Help me to understand! Why are multibyte characters not written to the file, only ascii characters are written, up to 255 and then everything from scratch?

#include <stdio.h>
#include <wchar.h>

int main (void)
{
  wchar_t num;
  FILE *file = fopen("text", "r");
  num = fgetwc(file);
  fclose(file);
  num++;
  FILE *file1 = fopen("text", "w");
  fputwc(num, file1);
  fclose(file1);
}

On Windows with Visual Studio Everything was recorded perfectly, I'm not sure exactly, but more than one byte of the value was in the file. With the GCC compiler, I can’t figure out why only ascii characters are written? Checked with the fwide() function returns 1 (the file is opened in multibyte mode). Really This Wise compiler is not on friendly terms with such things?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Dubrovin, 2016-08-10
@z3apa3a

On Windows, wchar_t is 16 bits and always UTF-16, on gcc under Linux it is 32 and potentially any encoding, it all depends on the locale. Therefore, you can't even count a single character in its entirety. You can use the -fshort-wchar switch for gcc, but in general, these are crutches.

A
abcd0x00, 2016-08-11
@abcd0x00

#include <stdio.h>
#include <locale.h>
#include <wchar.h>

int main(void)
{
    FILE *fp_in, *fp_out;
    wchar_t wch;

    setlocale(LC_ALL, "ru_RU.UTF-8");

    fp_in = fopen("file.txt", "r");
    wch = getwc(fp_in);
    fclose(fp_in);

    putwc(wch, stdout);
    wch++;
    putwc(wch, stdout);
    putwc(L'\n', stdout);

    fp_out = fopen("output.txt", "w");
    putwc(wch, fp_out);
    fclose(fp_out);

    return 0;
}

Conclusion
[[email protected] wch]$ .ansi wch.c -o wch
[[email protected] wch]$ ./wch
жз
[[email protected] wch]$ cat file.txt 
ж[[email protected] wch]$ cat output.txt 
з[[email protected] wch]$

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question