Simple reading file using ReadFile()

喜欢而已 提交于 2019-12-12 02:43:08

问题


Why this code doesn't output anything(exept info word)? File is exist.

hReadFile = CreateFile(L"indexing.xml",GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ |FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    wchar_t *wchr = new wchar_t[20];
    DWORD dw;
    ReadFile(hReadFile, wchr, sizeof(wchar_t) * 5, &dw, NULL);
    CloseHandle(hReadFile);
    wchr[dw/sizeof(wchar_t)] = L'\0';
    std::wcout << L"info " << wchr << L"     " << dw << std::endl;

回答1:


A Unicode file might start with an optional Byte Order Mark (BOM).

For UTF-16 the BOM tells which endianess is used in the the file.

Also the BOM can be used to destinguish between different Unicode encodings.

The example file from the OP obviously carries such a BOM as its first two bytes, as increasing the pointer (to the 2-byte sized wchar_t typed array) skips it and lets the data be printed.

std::wcout << L"info " << (wchr+1) << L" " << dw << std::endl;


来源:https://stackoverflow.com/questions/13046140/simple-reading-file-using-readfile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!