Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write files with (readable) UTF8 characters?

I read a file that has 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 it perfectly (this is shown in the Editbox when running the program):

Edit1->Text=C;

Result ===> "3021";"亜";"7";"ア アシア つ.ぐ T1 や つぎ つぐ"

The thing is that when I want to write this on 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 do I write the result (the one i can see in the program running) in a file?

I use C language on CodeGear C++Builder

Resolved thanks to Christoph and nobugz for the help

I changed this line

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

to this one and it worked. Thanks

fwrite(UTF8Encode(Edit8->Text).c_str(),UTF8Encode(Edit8->Text).Length(),1,FileOUT);
like image 860
Nek Avatar asked Feb 26 '26 17:02

Nek


1 Answers

I don't know the framework, but if you use UTF8Decode() after reading the file, shouldn't you use UTF8Encode() before writing?

like image 64
Christoph Avatar answered Feb 28 '26 08:02

Christoph



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!