Community login and other support tools will be unavailable Saturday May 3rd 9:00 am to 3:00 pm (EST) due to planned maintenance. Learn More
Read chinese string line by line from text in c or c++.
I want to read chinese string from text file using c or c++.
But when i converted from wstring to string.it seems not correct.
Please find below code which i wrote.
_setmode(_fileno(stdout), _O_U16TEXT);
wifstream wifs;
wstring txtline;
//Open file
wifs.open("C:\\Users\\Public\\abc.txt");
// We are going to read an UTF-8 file
wifs.imbue(locale(wifs.getloc(), new codecvt_utf8<wchar_t, 0x10ffff, consume_header>()));
std::vector<std::string> vectTextLine;
//read line by line
while(getline(wifs, txtline))
{
wcout << txtline << L'\n';
//convert wstring to string
std::string s1 = WStringToString(txtline);
}
wifs.close();
std::string WStringToString(const std::wstring& s)
{
std::string temp(s.length(), ' ');
std::copy(s.begin(), s.end(), temp.begin());
return temp;
}