C ++ WriteFile Unicode字符 [英] c++ WriteFile unicode characters

查看:163
本文介绍了C ++ WriteFile Unicode字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用WriteFile函数将wstring写入UTF-8文件。
我希望文件包含这些字符ÑÁ,但我得到这个 ...。

I'm trying to write a wstring into a UTF-8 file using WriteFile function. I want the file to have these characters "ÑÁ" but I'm getting this "�".

这里是代码

#include <iostream>
#include <cstdlib>
#include <sstream>
#include <string>
#include <fstream>
#include <windows.h>
#include <wchar.h>
#include <stdio.h>
#include <winbase.h>
using namespace std;

const char filepath [] = "unicode.txt";

int main ()
{ 
   wstring str;
   str.append(L"ÑÁ");
   wchar_t* wfilepath;

   // Create a file to work with Unicode and UTF-8
   ofstream fs;
   fs.open(filepath, ios::out|ios::binary);
   unsigned char smarker[3];
   smarker[0] = 0xEF;
   smarker[1] = 0xBB;
   smarker[2] = 0xBF;
   fs << smarker;
   fs.close();

   //Open and write in the file with windows functions
   mbstowcs(wfilepath, filepath, strlen(filepath));
   HANDLE hfile;
   hfile = CreateFileW(TEXT(wfilepath), GENERIC_WRITE, 0, NULL,
       OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
   wstringbuf strBuf (str, ios_base::out|ios::app);
   DWORD bytesWritten;
   DWORD dwBytesToWrite = (DWORD) strBuf.in_avail();
   WriteFile(hfile, &strBuf, dwBytesToWrite, &bytesWritten, NULL);
   CloseHandle(hfile);
}

我使用以下命令行在cygwin上进行编译:

g ++ -std = c ++ 11 -g Windows.C -o Windows

I compile it on cygwin using this command line:
g++ -std=c++11 -g Windows.C -o Windows

推荐答案

您需要先将UTF-16数据转换为UTF-8,然后再将其写入文件。

You need to convert the UTF-16 data to UTF-8 before writing it to the file.

并且无需使用以下命令创建文件 std :: ofstream ,将其关闭,然后使用 CreateFileW()重新打开。只需打开文件一次,然后写所有您需要的文件即可。

And there is no need to create the file with std::ofstream, close it, and re-open it with CreateFileW(). Just open the file once and write everything you need.

尝试一下:

#include <iostream>
#include <cstdlib>
#include <string>
//#include <codecvt>
//#include <locale>

#include <windows.h>
#include <wchar.h>
#include <stdio.h>

using namespace std;

LPCWSTR filepath = L"unicode.txt";

string to_utf8(const wstring &s)
{
    /*
    wstring_convert<codecvt_utf8_utf16<wchar_t>> utf16conv;
    return utf16conv.to_bytes(s);
    */

    string utf8;
    int len = WideCharToMultiByte(CP_UTF8, 0, s.c_str(), s.length(), NULL, 0, NULL, NULL);
    if (len > 0)
    {
        utf8.resize(len);
        WideCharToMultiByte(CP_UTF8, 0, s.c_str(), s.length(), &utf8[0], len, NULL, NULL);
    }
    return utf8;
}

int main ()
{ 
    wstring str = L"ÑÁ";

    // Create a UTF-8 file and write in it using Windows functions
    HANDLE hfile = CreateFileW(filepath, GENERIC_WRITE, 0, NULL,
       CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    if (hfile != INVALID_HANDLE_VALUE)
    {
        unsigned char smarker[3];
        DWORD bytesWritten;

        smarker[0] = 0xEF;
        smarker[1] = 0xBB;
        smarker[2] = 0xBF;
        WriteFile(hfile, smarker, 3, &bytesWritten, NULL);

        string strBuf = to_utf8(str);
        WriteFile(hfile, strBuf.c_str(), strBuf.size(), &bytesWritten, NULL);

        CloseHandle(hfile);
    }

    return 0;
}

这篇关于C ++ WriteFile Unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆