如何从wfstream读取二进制数据
How can I read binary data from 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_ttemplated?),才能读取下一个数据块。
显然,应该有一种更好的方式来实现我想要做的事情。
考虑到您的需求,我认为wfstream不是最好的选择。考虑使用类似以下代码片段的东西。
#include "stdafx.h"
#include <fstream>
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
std::wstring str(L"hello");
size_t size1 = str.length();
char data[] = { 0x10, 0x20, 0x30 };
size_t size2 = 3;
FILE* output = NULL;
if (_wfopen_s(&output, L"c:\test.bin", L"wb") == 0) {
fwrite(&size1, sizeof(size_t), 1, output);
fwrite(str.c_str(), size1 * sizeof(wchar_t), 1, output);
fwrite(&size2, sizeof(size_t), 1, output);
fwrite(data, size2, 1, output);
fclose(output);
}
FILE* input = NULL;
if (_wfopen_s(&input, L"c:\test.bin", L"rb") == 0) {
fread(&size1, sizeof(size_t), 1, input);
wchar_t* wstr = new wchar_t[size1 + 1];
fread(wstr, size1 * sizeof(wchar_t), 1, input);
std::wstring str(wstr, size1);
delete[] wstr;
fread(&size2, sizeof(size_t), 1, input);
char* data1 = new char[size2];
fread(data1, size2, 1, input);
std::wcout << str.c_str() << std::endl;
for (size_t i = 0; i < size2; ++i) {
std::wcout << std::hex << "0x" << int(data1[i]) << std::endl;
}
delete[] data1;
fclose(input);
}
return 0;
}
该输出:
hello
0x10
0x20
0x30
如果你想阅读宽字符,这将起作用:
wchar_t *comData = new wchar_t[comSize];
stream.read(comData, comSize);
另外,15字节的数据不能用宽流读取,因为最小的单元至少是2字节(见下文),所以你只能读取sizwof(wchar_t)*n的块。
但是,如果您关心应用程序的可移植性,wfstream/wchar_t可能不是最好的解决方案,因为没有标准wchar_t的宽度(例如,在windows上,wchar_t在许多unix/linux系统上是16位的,它是32位的)。
将文本存储为宽字符的第二个问题是endianes,我建议使用UTF-8进行文本存储。
# ifdef UNICODE
# define tfstream wfstream
# else
# define tfstream fstream
# endif
tfstream fs( _T("filename.bin"), tfstream::binary );
byte buffer[1023];
fs.read( buffer, sizeof(buffer) )
我认为,_T("filename.bin")和tfstream就是UI表达式;缓冲区和read()是DATA LOGIC表达式。wfstream不能将缓冲区限制为wchar_t类型。用户界面不得与数据逻辑混合!wfstream在这里做了错误的事情
- 如何从dicom文件中读取二进制数据
- 如何在Qt中从数据库中检索二进制数据?
- readsome() 适合在 Windows 上读取二进制数据吗?
- 如何使用 redis-plus-plus 存储二进制数据,就像我想存储结构一样?@for_stack?
- 将包含二进制数据的 QByteArray 传递到按值运行
- 如何在 c++ 中生成十六进制二进制数据的 sha256 哈希?
- 在处理网络、二进制数据和序列化时应使用流或容器
- 我能确定从文件中读取的 32 字节二进制数据等于 256 位吗?
- C++:如何通过 curl 调用使用 HTTP post 请求发送二进制数据(protobuf 数据)
- 使用二进制数据更新 PostgreSQL 表
- 使用二进制数据和无符号字符
- sd_journal_send发送二进制数据.如何使用日志检索数据?
- 从带有 std::ifstream::read() 的文件中读取 int 遍历 char * 二进制数据
- 将文本和二进制数据连接到一个文件中
- 二进制模式 + 格式化文本操作或文本模式 + 二进制数据操作 - 有意义吗?
- 将整数的二进制数据转换为浮点数
- 使用 CMake 在可执行文件中嵌入二进制数据
- 二进制数据作为命令行参数
- 如何访问文件的二进制数据?
- 返回二进制数据的通用方式,而无需原始指针