将二进制数据写入文件

发布于 2024-12-07 17:27:10 字数 1651 浏览 0 评论 0原文

我想在将二进制数据写入文件之前暂时缓存它。这是我的想法。

由于我必须在此数据之前插入一个标头,以指示标头后面将跟随多少数据,因此我需要一种方法来在将这些数据写入 ofstream 文件 之前对其进行缓存。我决定创建 ostream buffer(); ,其中我可以转储所有这些数据而不将其写入文件。

写入标题后,我只需执行 file << buffer 来转储数据。

我仍在努力解决编译器错误,例如以下错误:

error: no matching function for call to ‘TGA2Converter::writePixel(std::ostream (&)(), uint32_t&)’
note: candidate is: void TGA2Converter::writePixel(std::ostream&, uint32_t)

为什么我会收到此消息?也许更重要的是,我是否以最有效、最方便的方式解决这个问题?


编辑:人们一直在索要代码。我试图将范围缩小到这个......

// This is a file. I do not want to write the binary
// data to the file before I can write the header.
ofstream file("test.txt", ios::binary);

// This is binary data. Each entry represents a byte.
// I want to write it to a temporary cache. In my
// real code, this data has to be gathered before
// I can write the header because its contents depend
// on the nature of the data.
stringstream cache;
vector<uint32_t> arbitraryBinData;
arbitraryBinData.resize(3);
arbitraryBinData[0] = 0x00;
arbitraryBinData[1] = 0xef;
arbitraryBinData[2] = 0x08;

// Write it to temporary cache
for (unsigned i = 0; i < arbitraryBinData.size(); ++i)
    cache << arbitraryBinData[i];

// Write header
uint32_t header = 0x80;     // Calculation based on the data!
file << header;

// Write data from cache
file << cache;

我完全期望这个二进制数据被写入文件:

0000000: 8000 ef08

但我得到了这个:

0000000: 3132 3830 7837 6666 6638 6434 3764 3139
0000010: 38

为什么我没有得到预期的结果?

I want to temporarily cache binary data before I can write it to a file. This was my idea.

Since I have to insert a header before this data that indicates how much data will follow after the header, I needed a way to keep this data cached before it is written to ofstream file. I decided to create ostream buffer(); wherein I could dump all this data without writing it to the file.

After the header was written, I'd just do file << buffer to dump the data.

I'm still struggling with compiler errors such as this one:

error: no matching function for call to ‘TGA2Converter::writePixel(std::ostream (&)(), uint32_t&)’
note: candidate is: void TGA2Converter::writePixel(std::ostream&, uint32_t)

Why am I getting this message? And, perhaps more importantly, am I approaching the problem in the most effective and convenient way?


Edit: people have been asking for code. I tried to narrow it down to this...

// This is a file. I do not want to write the binary
// data to the file before I can write the header.
ofstream file("test.txt", ios::binary);

// This is binary data. Each entry represents a byte.
// I want to write it to a temporary cache. In my
// real code, this data has to be gathered before
// I can write the header because its contents depend
// on the nature of the data.
stringstream cache;
vector<uint32_t> arbitraryBinData;
arbitraryBinData.resize(3);
arbitraryBinData[0] = 0x00;
arbitraryBinData[1] = 0xef;
arbitraryBinData[2] = 0x08;

// Write it to temporary cache
for (unsigned i = 0; i < arbitraryBinData.size(); ++i)
    cache << arbitraryBinData[i];

// Write header
uint32_t header = 0x80;     // Calculation based on the data!
file << header;

// Write data from cache
file << cache;

I fully expected this binary data to be written to the file:

0000000: 8000 ef08

But I got this:

0000000: 3132 3830 7837 6666 6638 6434 3764 3139
0000010: 38

Why am I not getting the expected result?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

回眸一笑 2024-12-14 17:27:10

ostream buffer(); 声明一个名为 buffer 的函数,不带任何参数并返回一个 ostream。此外,ostream 是一个基类,您应该使用 strstream 代替。

ostream buffer(); is declaring a function called buffer taking no arguments and returning an ostream. Also ostream is a base class, you should be using strstream instead.

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