Write a unicode CString to a file using WriteFile

2019-04-15 07:10发布

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); 

?

5条回答
Evening l夕情丶
2楼-- · 2019-04-15 07:29

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.

查看更多
太酷不给撩
3楼-- · 2019-04-15 07:37

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.

查看更多
趁早两清
4楼-- · 2019-04-15 07:43

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.

查看更多
手持菜刀,她持情操
5楼-- · 2019-04-15 07:46

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

查看更多
男人必须洒脱
6楼-- · 2019-04-15 07:50

I believe you need additional casting:

WriteFile(handle, (LPCVOID)(LPCTSTR)cstr, cstr.GetLength() * sizeof(TCHAR), &dwWritten, NULL);
查看更多
登录 后发表回答