使用 ReadFile() 简单读取文件 [英] Simple reading file using ReadFile()

查看:30
本文介绍了使用 ReadFile() 简单读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这段代码不输出任何东西(除了信息字)?文件存在.

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;

推荐答案

Unicode 文件可能以可选的 开头字节顺序标记 (BOM).

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

对于 UTF-16,BOM 说明文件中使用的字节顺序.

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

还可以使用 BOM 来区分不同的 Unicode 编码.

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

来自 OP 的示例文件显然带有这样的 BOM 作为其前两个字节,因为增加指针(指向 2 字节大小的 wchar_t 类型数组)跳过它并让数据打印.

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;

这篇关于使用 ReadFile() 简单读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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