Created
May 18, 2021 09:29
-
-
Save aliakseis/d56d90f16ec7ff54e134264a12ede375 to your computer and use it in GitHub Desktop.
Converting a file from ANSI to utf8
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ansi2utf8.cpp : This file contains the 'main' function. Program execution begins and ends there. | |
| // | |
| #include <iostream> | |
| #include <string> | |
| #include <fstream> | |
| #include <streambuf> | |
| #include <atlcore.h> | |
| #include <atlconv.h> | |
| int wmain(int argc, wchar_t *argv[]) | |
| { | |
| if (argc != 3) | |
| { | |
| std::cerr << "Wrong parameters number.\n"; | |
| return 1; | |
| } | |
| std::ifstream t(argv[1]); | |
| std::string str; | |
| t.seekg(0, std::ios::end); | |
| str.reserve(t.tellg()); | |
| t.seekg(0, std::ios::beg); | |
| str.assign((std::istreambuf_iterator<char>(t)), | |
| std::istreambuf_iterator<char>()); | |
| std::ofstream oft(argv[2]); | |
| const char tag[] = { char(0xEF), char(0xBB), char(0xBF) }; | |
| oft << tag; | |
| oft << static_cast<const char*>(ATL::CW2A(ATL::CA2W(str.c_str(), CP_ACP), CP_UTF8)); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment