簡體   English   中英

如何從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_t模板化?)以便能夠讀取下一個數據塊。

顯然, 應該有一種更好的方式來實現我想要做的事情。

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沒有標准的寬度(例如,在許多unix / linux系統上,windows wchar_t是16位,它是32位)。

將文本存儲為寬字符的第二個問題是endianess,我建議使用UTF-8進行文本存儲。

考慮到您的要求,我認為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
# 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類型。 用戶界面不得與DATA LOGIC混合使用! wfstream在這里做錯了

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM