I want to transfer and show an string from a control in VC++ to a file.
in this way, I use the WideCharToMultiByte()
function. But there is a problem!
This is my code:
CString m_sName=L"ABC";
int size_needed=WideCharToMultiByte(CP_UTF8, 0, m_sName, -1, NULL, 0, NULL, NULL);
std::string utf8_string(size_needed, 0);
WideCharToMultiByte(CP_UTF8, 0, m_sName, -1, &utf8_string[0], size_needed, NULL, NULL);
FILE *file;
fopen_s(&file, "bid.txt", "wb");
fprintf(file, "\nutf8_string[0]=%c", utf8_string[0]);
fprintf(file, "\nutf8_string[1]=%c", utf8_string[1]);
fprintf(file, "\nutf8_string[2]=%c", utf8_string[2]);
fprintf(file, "\nutf8_string=%s", utf8_string);
fclose(file);
When I open the bid.txt
file, I can see:
utf8_string[0]=A
utf8_string[1]=B
utf8_string[2]=C
but:
utf8_string=NULL
instead of utf8_string=ABC
this is my problem. WHY?!!