如何从wfstream读取二进制数据?

时间:2020-03-06 15:01:16  来源:igfitidea点击:

我从文件中读取数据时遇到一个小问题。我希望能够读取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模板化?)才能读取下一个数据块。

显然,应该有一种更好的方法来实现我的目标。

解决方案

考虑到要求,我认为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位)。

将文本存储为宽字符的第二个问题是字节序,我建议使用UTF-8进行文本存储。