Советую вам не использовать utf-8 при выводе в консоль на Windows (режим utf-8 имеет баги), вместо этого пользоваться "широкими" потоками (подробнее про использование юникода в Windows)
#include <iostream>
#include <windows.h>
#include <fcntl.h>
#include <io.h>
#include <iostream> // std::wcerr
std::wstring strtows(const std::string &str, UINT codePage)
{
std::wstring ws;
int n = MultiByteToWideChar(codePage, 0, str.c_str(), static_cast<int>(str.size()), NULL, 0);
if (n) {
ws.resize(n);
if (MultiByteToWideChar(codePage, 0, str.c_str(), static_cast<int>(str.size()), &ws[0], n) == 0)
ws.clear();
}
return ws;
}
std::string wstostr(const std::wstring &ws, UINT codePage)
{
// prior to C++11 std::string and std::wstring were not guaranteed to have their memory be contiguous,
// although all real-world implementations make them contiguous
std::string str;
int srcLen = static_cast<int>(ws.size());
int n = WideCharToMultiByte(codePage, 0, ws.c_str(), srcLen, NULL, 0, 0, NULL);
if (n) {
str.resize(n);
if (WideCharToMultiByte(codePage, 0, ws.c_str(), srcLen, &str[0], n, 0, NULL) == 0)
str.clear();
}
return str;
}
std::string WstringToUtf8(const std::wstring &str)
{
return wstostr(str, CP_UTF8);
}
std::wstring Utf8ToWstring(const std::string &str)
{
return strtows(str, CP_UTF8);
}
int main(int argc, char *argv[])
{
_setmode(_fileno(stdout), _O_U16TEXT);
std::wcout << Utf8ToWstring(u8"это строка6") << std::endl;
return 0;
}
_setmode(_fileno(stdout), _O_U8TEXT);работает? (взято отсюда) – VladD Jul 07 '15 at 15:15test.cppкомандойmake test CPPFLAGS=-std=c++0xбинарник при запуске не выдаёт никакик bom-ов. первые байты:d1 8d d1 82 d0 be 20 d1, вполне валидный utf8. – aleksandr barakin Jul 07 '15 at 15:43std::cout << u8" это строка6" << std::endl;(с пробелом в начале) выводится нормально. – Sergey S Jul 07 '15 at 16:46puts("\302\260")выводит символ корректно, аputc('\302', stdout); putc('\260', stdout);- нет. – Sergey S Jul 07 '15 at 18:35