简体   繁体   English

使用ReadFile()的简单读取文件

[英]Simple reading file using ReadFile()

Why this code doesn't output anything(exept info word)? 为什么这段代码不输出任何内容(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;

A Unicode file might start with an optional Byte Order Mark (BOM) . Unicode文件可能以可选的字节顺序标记(BOM)开头。

For UTF-16 the BOM tells which endianess is used in the the file. 对于UTF-16,BOM会告知文件中使用了哪个字节序。

Also the BOM can be used to destinguish between different Unicode encodings. 此外,BOM可用于在不同的Unicode编码之间进行转发。

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. 来自OP的示例文件显然带有这样一个BOM作为其前两个字节,因为增加指针(到2字节大小的wchar_t类型数组)跳过它并让数据被打印。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM