-
c++로 텍스트 파일 쓸 때 참고프로그래밍/c++ 2017. 6. 15. 01:46반응형
utf8 파일을 읽고 utf16으로 변환하는 예제
std::codecvt http://en.cppreference.com/w/cpp/locale/codecvt
codecvt_utf8, codecvt_utf16, codecvt_utf8_utf16 전부 c++ 17부터 deprecated 되었다네?ㅋㅋ 꼬시다
이렇게 하면 윈도우에서 c++ wofstream과 wide character를 써서 Unicode LE 로 저장할 수 있다.(단, bom이 없음)
using namespace std;
wofstream ofs("Hello.txt", std::ios::binary);
ofs.imbue(std::locale(ofs.getloc(), new std::codecvt_utf16<wchar_t, 0x10ffff, std::little_endian>));
ofs << L"123ㅋㅋ";
setf 관련
Need for noskipws on stream with T=unsigned char in binary mode?
https://stackoverflow.com/questions/21803615/need-for-noskipws-on-stream-with-t-unsigned-char-in-binary-mode
=> The basic algorithm for >> of a string is:
1) skip whitespace
2) read and extract until next whitespace#include <iostream>#include <fstream>std::string read_file(const std::string& fileName){std::ifstream ifs(fileName);ifs.unsetf(std::ios::skipws); // no whitespace skipping(space, tap, enter)return std::string( // 그렇지만 이런식으로 하면 의미 없다.std::istreambuf_iterator<char>(ifs.readbuf()),std::istreambuf_iterator<char>());}int main(){return 0;}반응형'프로그래밍 > c++' 카테고리의 다른 글
asio 기반으로 라이브러리 프로토타입을 만들었다. (0) 2017.12.24 구글 플랫버퍼(flatbuffers) 맛보기 in c++ (1) 2017.11.30 c++에서 람다를 함수 인자로 쓸 때 적절한 방법 (0) 2016.10.15 boost::variant(std::variant c++17) 사용하기 (4) 2016.08.05 boost::any에서의 unsafe_any_cast (3) 2016.06.06