これが私が最初に同等であると思ったコードの2つのスニペットです:
{
std::ifstream stream("test.bin", std::ios_base::in | std::ios_base::binary);
unsigned char count = 128;
unsigned char read = 0;
unsigned char scanline[128];
long long start = stream.tellg();
while (count--) {
stream >> scanline[read++]; // <---- This is the only line which differs
}
long long end = stream.tellg();
std::cout << end - start << "\n";
}
{
std::ifstream stream("test.bin", std::ios_base::in | std::ios_base::binary);
unsigned char count = 128;
unsigned char read = 0;
unsigned char scanline[128];
long long start = stream.tellg();
while (count--) {
stream.read((char*)&scanline[read++], 1); // <---- This is the only line which differs
}
long long end = stream.tellg();
std::cout << end - start << "\n";
}
私の問題は、最初のバージョンが153(おそらく入力データに依存)を出力し、2番目のバージョンが128(私が期待したもの)を出力することです。これは、最初のバージョンでデータが抽出される方法と関係があるはずですが、なぜそれが機能しないのか理解できません。それは単に呼び出すべきではありません:
istream& operator>> (istream& is, unsigned char& ch);
毎回fileposを1バイト移動しますか?