如何从 wfstream 读取二进制数据?
我从文件读取数据时遇到一个小问题。 我希望能够读取 wstring 以及任意大小的原始数据块(大小以字节为单位)。
std::wfstream stream(file.c_str());
std::wstring comType;
stream >> comType;
int comSize;
stream >> comSize;
char *comData = new char[comSize];
memset(comData, 0, comSize);
stream.read(comData, comSize);
//error C2664 : 'std::basic_istream<_Elem,_Traits>::read'
// : cannot convert parameter 1 from 'char *' to 'wchar_t *'
也许我使用了错误的流,或者类似的东西。 基本上,我想读取一个 wstring,后面跟着数据的大小(可以是任意数量的字节),后面跟着那么多字节的组件数据。 显然,我无法读取 char,因为模板假定为 wchar_t。
我可以读取 wchar_t,但是我必须确保数据按 sizeof(wchar_t) 对齐存储。 否则,我最终可能会破坏流。 一种情况是数据为 15 字节。 我必须读取 16 个字节,然后屏蔽不需要的字节,将流查找到 15 个字节偏移量(如果可能的话使用 wchar_t 模板化?)才能读取下一个数据块。
显然,应该有一种更好的方法来实现我想要做的事情。
I have a slight problem reading data from file. I want to be able to read wstring's, aswell as a chunk of raw data of arbitrary size (size is in bytes).
std::wfstream stream(file.c_str());
std::wstring comType;
stream >> comType;
int comSize;
stream >> comSize;
char *comData = new char[comSize];
memset(comData, 0, comSize);
stream.read(comData, comSize);
//error C2664 : 'std::basic_istream<_Elem,_Traits>::read'
// : cannot convert parameter 1 from 'char *' to 'wchar_t *'
Perhaps I am using wrong streams, or something along those lines. Basically, I want to read a wstring, size of the data followed (which could be any number of bytes), followed by that many bytes of component data. Obviously, I can't read char's because the template assumes wchar_t's.
I can read wchar_t's but then I have to make sure the data is stored as aligned by sizeof(wchar_t). Otherwise, I could end up corrupting the stream. A scenario would be when the data is 15 bytes. I would have to read 16 bytes, then mask the unwanted byte, seek the stream to 15 bytes offset (if possible with wchar_t templated?) to be able to read the next data chunk.
Clearly, there should be a nicer way of achieving what I am trying to do.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
考虑到您的要求,我认为 wfstream 不是正确的选择。 考虑使用类似以下代码片段的内容。
这输出:
Considering your requirements I do not think wfstream is the way to go. Considerer using something like the following code snippet.
This outputs:
Stream.read 的问题在于它使用 wchar_t 作为 wfstream 的“字符单元”。 如果您使用 fstream,它会使用 char 作为“字符单位”。
如果你想读取宽字符,这会起作用:
同样,15字节的数据不能用宽流读取,因为最小单位至少是2字节(见下文),所以你只能读取sizwof(wchar_t)的块* n.
但如果您担心应用程序的可移植性,wfstream/wchar_t 可能不是最好的解决方案,因为没有标准 wchar_t 的宽度(例如,在 Windows 上 wchar_t 是 16 位,在许多 unix/linux 系统上它是 32 位)。
将文本存储为宽字符的第二个问题是字节顺序,我建议使用 UTF-8 进行文本存储。
the problem with the stream.read is that it uses wchar_t as "character unit" with wfstream. If you use fstream it uses char as "character unit".
This would work if you want to read wide characters:
Also 15 bytes of data can't be read with a wide stream, because the smallest unit is at least 2bytes (see below), so you can only read chunks of sizwof(wchar_t) * n.
But if you are concerned about portability of the application wfstream/wchar_t is maybe not the best solution because there is no standard how wide wchar_t is (e.g. on windows wchar_t is 16bit on many unix/linux systems it is 32bit).
The second problem with storing text as wide characters is endianess, i would suggest to use UTF-8 for text storage.
我认为, _T("filename.bin") 和 tfstream 是 UI 表达式; 缓冲区和 read() 是数据逻辑表达式。 wfstream 不得将缓冲区限制为 wchar_t 类型。 UI 不得与数据逻辑混合! wfstream 在这里做了错误的事情
I think, the _T("filename.bin") and tfstream are the UI expression; the buffer and the read() is DATA LOGIC expression. wfstream must NOT restrict the buffer to the type wchar_t. The UI must NOT mix with DATA LOGIC ! wfstream do the wrong thing here