バイナリ データをファイルに書き込む前に、一時的にキャッシュしたいと考えています。これが私の考えでした。
このデータの前に、ヘッダーの後に続くデータの量を示すヘッダーを挿入する必要があるため、このデータを に書き込む前にキャッシュしておく方法が必要でしたofstream file
。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
期待した結果が得られないのはなぜですか?