Skip to content

Instantly share code, notes, and snippets.

@leafbird
Created June 25, 2013 15:25
Show Gist options
  • Save leafbird/5859381 to your computer and use it in GitHub Desktop.
Save leafbird/5859381 to your computer and use it in GitHub Desktop.
std::fstream 계열로 파일 I/O를 할 때 utf-8인코딩을 사용하도록 설정하는 코드. 출처 : http://sockbandit.wordpress.com/2012/05/31/c-read-and-write-utf-8-file-using-standard-libarary/
#include <fstream>
#include <iostream>
#include <string>
#include <locale>
#include <codecvt>
...
// Write file in UTF-8
std::wofstream wof;
wof.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t,0x10ffff,std::generate_header>));
wof.open(L"file.txt");
wof << L"This is a test.";
wof << L"This is another test.";
wof << L"\nThis is the final test.\n";
wof.close();
// Read file in UTF-8
std::wifstream wif(L"file.txt");
wif.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t,0x10ffff, std::consume_header>));
std::wstringstream wss;
wss << wif.rdbuf();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment