使用 WriteFile API 将 unicode CString 写入文件

发布于 2024-12-05 20:06:34 字数 357 浏览 0 评论 0原文

如何使用 WriteFile Win32 API 函数将 CString 实例的内容写入由 CreateFile 打开的文件?

请注意,不使用 MFC,而是通过包含“atlstr.h”来使用 CString

编辑:我可以做

WriteFile(handle, cstr, cstr.GetLength(), &dwWritten, NULL); 

or 吗

WriteFile(handle, cstr, cstr.GetLength() * sizeof(TCHAR), &dwWritten, NULL); 

How can I write contents of a CString instance to a file opened by CreateFile using WriteFile Win32 API function?

Please note MFC is not used, and CString is used by including "atlstr.h"

edit: Can just I do

WriteFile(handle, cstr, cstr.GetLength(), &dwWritten, NULL); 

or

WriteFile(handle, cstr, cstr.GetLength() * sizeof(TCHAR), &dwWritten, NULL); 

?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦里寻她 2024-12-12 20:06:34

对于 ATL,它是这样的:

CString sValue;
CStringW sValueW(sValue); // NOTE: CT2CW() can be used instead

CAtlFile File;
ATLENSURE_SUCCEEDED(File.Create(sPath, GENERIC_WRITE, ...));
static const BYTE g_pnByteOrderMark[] = { 0xFF, 0xFE }; // UTF-16, Little Endian
ATLENSURE_SUCCEEDED(File.Write(g_pnByteOrderMark, sizeof g_pnByteOrderMark));
ATLENSURE_SUCCEEDED(File.Write(sValueW, (DWORD) (sValueW.GetLength() * sizeof (WCHAR))));

它是字节顺序标记 (BOM),它让记事本知道编码是 UTF-16。

With ATL it's like this:

CString sValue;
CStringW sValueW(sValue); // NOTE: CT2CW() can be used instead

CAtlFile File;
ATLENSURE_SUCCEEDED(File.Create(sPath, GENERIC_WRITE, ...));
static const BYTE g_pnByteOrderMark[] = { 0xFF, 0xFE }; // UTF-16, Little Endian
ATLENSURE_SUCCEEDED(File.Write(g_pnByteOrderMark, sizeof g_pnByteOrderMark));
ATLENSURE_SUCCEEDED(File.Write(sValueW, (DWORD) (sValueW.GetLength() * sizeof (WCHAR))));

It's byte order mark (BOM) which lets Notepad know that encoding is UTF-16.

只等公子 2024-12-12 20:06:34

您需要选择一种文本编码,将字符串转换为该编码,然后相应地写入文本文件。最简单的是 ANSI,因此基本上您可以使用 T2CA 宏将 TCHAR 字符串转换为 ansi,然后将内容转储到文件中。类似于(未经测试/编译):

// assumes handle is already opened in an empty new file
void DumpToANSIFile(const CString& str, HANDLE hFile)
{
    USES_CONVERSION;
    PCSTR ansi = T2CA(str);
    DWORD dwWritten;
    WriteFile(hFile, ansi, strlen(ansi) * sizeof(ansi[0]), &dwWritten, NULL);
}               

因为它是 ANSI 编码,所以它只能在具有相同代码页设置的计算机上读取。要获得更便携的解决方案,请使用 UTF-8 或 UTF-16。

You need to pick a text encoding, convert the string to that encoding, and write the text file accordingly. Simplest is ANSI, so basically you would use the T2CA macro to convert your TCHAR string to ansi, then dump the contents in a file. Something like (untested/compiled):

// assumes handle is already opened in an empty new file
void DumpToANSIFile(const CString& str, HANDLE hFile)
{
    USES_CONVERSION;
    PCSTR ansi = T2CA(str);
    DWORD dwWritten;
    WriteFile(hFile, ansi, strlen(ansi) * sizeof(ansi[0]), &dwWritten, NULL);
}               

Because it's ANSI encoding though, it will only be readable on computers that have your same code page settings. For a more portable solution, use UTF-8 or UTF-16.

梦境 2024-12-12 20:06:34

转换为 ANSI 可能会导致代码页出现问题,因此在许多情况下是不可接受的。下面是一个将 unicode 字符串保存到 unicode 文本文件的函数:

void WriteUnicodeStringToFile(const CString& str, LPCWSTR FileName)
{
HANDLE f = CreateFileW(FileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (f == INVALID_HANDLE_VALUE) return; //failed
DWORD wr;
unsigned char Header[2]; //unicode text file header
Header[0] = 0xFF;
Header[1] = 0xFE;
WriteFile(f, Header, 2, &wr, NULL);
WriteFile(f, (LPCTSTR)str, str.GetLength() * 2, &wr, NULL); 
CloseHandle(f);
}

使用:

CString str = L"This is a sample unicode string";
WriteUnicodeStringToFile(str, L"c:\\Sample.txt");

记事本可以识别 unicode 文本文件。

Converting to ANSI can cause problems with code pages, so it is not acceptable in many cases. Here is a function that saves unicode string to unicode text file:

void WriteUnicodeStringToFile(const CString& str, LPCWSTR FileName)
{
HANDLE f = CreateFileW(FileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (f == INVALID_HANDLE_VALUE) return; //failed
DWORD wr;
unsigned char Header[2]; //unicode text file header
Header[0] = 0xFF;
Header[1] = 0xFE;
WriteFile(f, Header, 2, &wr, NULL);
WriteFile(f, (LPCTSTR)str, str.GetLength() * 2, &wr, NULL); 
CloseHandle(f);
}

Using:

CString str = L"This is a sample unicode string";
WriteUnicodeStringToFile(str, L"c:\\Sample.txt");

Notepad understands unicode text files.

纵山崖 2024-12-12 20:06:34

我相信你需要额外的铸造:

WriteFile(handle, (LPCVOID)(LPCTSTR)cstr, cstr.GetLength() * sizeof(TCHAR), &dwWritten, NULL);

I believe you need additional casting:

WriteFile(handle, (LPCVOID)(LPCTSTR)cstr, cstr.GetLength() * sizeof(TCHAR), &dwWritten, NULL);
万人眼中万个我 2024-12-12 20:06:34

还要确保在开头写入正确的字节顺序标记,以便记事本可以正确读取它。

Also make sure that you write proper byte order mark in the beginning so that Notepad can read it properly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文