如何从wfstream读取二进制数据

How can I read binary data from wfstream?

本文关键字:二进制 数据 读取 wfstream      更新时间:2023-10-16

我在从文件中读取数据时遇到了一个小问题。我希望能够读取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
stream.read的问题在于它使用wchar_t作为wfstream的"字符单元"。如果使用fstream,则使用char作为"字符单元"。

如果你想阅读宽字符,这将起作用:

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在这里做了错误的事情