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

查看:160
本文介绍了使用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天全站免登陆