Reputation: 18575
I have a file.txt encoded in gbk, I read some bytes through ifstream::read
and store them into a char
buffer, then I want to print every word in the buffer in gbk
.
I assume wchar_t
is needed here, so I do it like this:
int main()
{
ifstream ifs("./file.txt");
char buf[256];
ifs.read(buf, 255);
wchar_t wbuf[256];
mbstowcs( wbuf, buf, 255);
for (int i = 0; i < wcslen(wbuf); i++)
wprintf(L"%c ", wbuf[i]);
}
Am I doing it right? Or any better idea to do the job?
Thanks.
Upvotes: 0
Views: 813
Reputation: 204668
Have you tried using the wide-char specializations? (ifstream
and wifstream
are template specializations of basic_ifstream
over char
and wchar_t
respectively.)
wifstream wifs("./file.txt");
wifs.imbue(locale("zh_CN.GBK"));
wchar_t wbuf[256];
wifs.read(buf, 255);
Upvotes: 2