Thiago R. Adams website

Home Code-Blog Projects Twitter Blog Links / Books About

Websites

Sample of use of codecvt_utf8.

This class is part of C++0x. "Standard code conversion facets"


#include <fstream>
#include <codecvt>
#include <string>
using namespace std;

int main()
{

        // writing
	{
		std::locale ulocale(locale(), new codecvt_utf8<wchar_t>) ;
		std::wofstream ofs("test.txt");
		ofs.imbue(ulocale);
		ofs << L"maçã"; //apple in portuguese
        
        }

        // reading
	{
		std::locale ulocale(locale(), new codecvt_utf8<wchar_t>) ;
		std::wifstream ifs("test.txt");
		ifs.imbue(ulocale);
		std::wstring ws;
		std::getline(ifs, ws);		
    }
}

(compiled using VC++ 2010 express)

Want to see more? Go to the CodeBlog section.

About the author: I am Thiago Adams. I work as a professional C++ software engineer. I have created this website to share ideas and source code with other people with similar interests.
I would like to hear from you comments, critics, questions and suggestions about this topic or any other part of this website. Email: thiago.adams at gmail dot com