Re: Is it difficult to read UTF-8 .txt in VC++?
On Fri, 06 Jul 2007 19:47:41 +0200, Sin Jeong-hun <firstname.lastname@example.org> =
I want to read a line from a .txt file (which is saved in
Unicode(UTF-8) and show the string in a MessageBox.
With C#, it is very easy.
StreamReader sr=new StreamReader("c:\\a.txt",Encoding.UTF8);
But with unmanaged Visual C++, I couldn't even find a simple example.
Do I really need to buy a library just to read simple UTF-8 text file?=
No, you don't need to buy anything.
For solving problems like this, I usually use UTF8-CPP library.
It's just a single header file (+ 3 utility headers), no binaries.
1. Go to the website and download UTF8-CPP 2.0
2. Unpack and add utf8 directory to "Additional Include Directories"
of your project.
3. Use it as follows (and read manual for more complete reference):
// Project properties: General -> Character Set -> Unicode
#include <utf8.h> // UTF8-CPP
#include <windows.h> // MessageBox()
int _tmain(int argc, _TCHAR* argv)
char const* path = "utf8.html"; // UTF-8 encoded
// Read UTF-8 file
assert(fs8.is_open() && "Could not open");
assert(utf8::is_valid(line.begin(), line.end()) && "Invalid UTF-8 =
// Convert UTF-8 string to UTF-16
utf8::utf8to16(line.begin(), line.end() , std::back_inserter(msg));=
::MessageBox(NULL, msg.c_str(), _T("Test"), MB_OK);