Skip to content

Instantly share code, notes, and snippets.

@aliakseis
Created May 18, 2021 09:29
Show Gist options
  • Select an option

  • Save aliakseis/d56d90f16ec7ff54e134264a12ede375 to your computer and use it in GitHub Desktop.

Select an option

Save aliakseis/d56d90f16ec7ff54e134264a12ede375 to your computer and use it in GitHub Desktop.
Converting a file from ANSI to utf8
// 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