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

发布于 2024-07-07 07:07:16 字数 782 浏览 6 评论 0原文

我从文件读取数据时遇到一个小问题。 我希望能够读取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

终难遇 2024-07-14 07:07:16

考虑到您的要求,我认为 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

Considering your requirements I do not think wfstream is the way to go. Considerer using something like the following code snippet.

#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;
}

This outputs:

hello
0x10
0x20
0x30
蒗幽 2024-07-14 07:07:16

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 是 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:

wchar_t *comData = new wchar_t[comSize];
stream.read(comData, comSize);

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.

娇纵 2024-07-14 07:07:16
# 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() 是数据逻辑表达式。 wfstream 不得将缓冲区限制为 wchar_t 类型。 UI 不得与数据逻辑混合! wfstream 在这里做了错误的事情

# 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) )

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文