Answer the question
In order to leave comments, you need to log in
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);
}
Answer the question
In order to leave comments, you need to log in
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.
#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;
}
[[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 questionAsk a Question
731 491 924 answers to any question