Reputation: 21
I write the code to create the . CSV files out there with Thai characters. But when I open a file using Microsoft Excel Thai characters in that file a wrong.But when I open it in Notepad, and then I press Save. And open it in Excel again. It is desired I think it is because the program does not Encoding to utf-8. I had to do to Program, save it as utf-8.
std:: ofstream MyCSVFile;
MyCSVFile.open("myfile.csv", std::ios::out | std::ios::app);
MyCSVFile << "Name,Address" << endl;
MyCSVFile <<name<<","<<address << endl;
MyCSVFile.close();
}
Upvotes: 0
Views: 2516
Reputation: 4235
You need to do the following (assuming the file path is stored in FilePath
):
Here is the code you should use:
const std::wstring fileStr(FilePath);
wofstream mFile(FilePath);
mFile.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t>));
if (mFile.is_open())
{
const wchar_t *bom = L"\xef\xbb\xbf";
mFile << bom;
...
Now you can write the text, and of course close the file.
Upvotes: 1
Reputation: 2241
You need to write the BOM to the beginning of the file. Try this:
const char *bom = "\xef\xbb\xbf";
MyCSVFile << bom;
MyCSVFile << "Name...
This a good read: BOM in Wikipedia.
Upvotes: 1