gpt4 book ai didi

c++ WriteFile unicode字符

转载 作者:行者123 更新时间:2023-11-30 01:49:42 26 4
gpt4 key购买 nike

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

这是代码

#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

最佳答案

在写入文件之前,您需要将 UTF-16 数据转换为 UTF-8。

并且不需要使用 std::ofstream 创建文件,关闭它,然后使用 CreateFileW() 重新打开它。只需打开文件一次并写入您需要的所有内容。

试试这个:

#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字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28618715/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com