How to write files with (readable) UTF8 characters?

I read a file with utf8 characters like this:

FILE *FileIN,*FileOUT;
FileIN=fopen("filename","r");
char string[600];
WideChar C[600],S[100];
fgets(string,600,FileIN);
wcscpy(C,UTF8Decode(string).c_bstr()); // widechar copy

And it reads perfectly (this is shown in Editbox when the program starts):

Edit1->Text=C;

Result ===> "3021";"亜";"7";"γ‚’ γ‚’γ‚·γ‚’ ぀.ぐ T1 γ‚„ ぀ぎ ぀ぐ"

The thing is, when I want to write this to a file:

FileOUT=fopen("txt.txt","w");    
fwrite(Edit8->Text.c_str(),strlen(Edit8->Text.c_str()),1,FileOUT);

Result ===> "3021";"?";"7";"? ??? ?.? T1 ? ?? ??"

The question is how to write the result (the one that I see in the running program) in a file?

I am using C language on CodeGear C ++ Builder

Resolved thanks to Christophe and nobility for helping

I changed this line

fwrite(Edit8->Text.c_str(),strlen(Edit8->Text.c_str()),1,FileOUT);

to this, and it worked. Thanks

fwrite(UTF8Encode(Edit8->Text).c_str(),UTF8Encode(Edit8->Text).Length(),1,FileOUT);
+3
source share
1 answer

I do not know the scope, but if you use UTF8Decode()after reading the file, should you not use it UTF8Encode()before writing?

+2
source

Source: https://habr.com/ru/post/1703356/


All Articles