使用 WriteFile API 将 unicode CString 写入文件 [英] Write a unicode CString to a file using WriteFile API

查看:32
本文介绍了使用 WriteFile API 将 unicode CString 写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

请注意未使用 MFC,通过包含atlstr.h"使用 CString

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

我可以吗

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

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

?

推荐答案

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.

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

这篇关于使用 WriteFile API 将 unicode CString 写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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