我正在使用Ubuntu 10.04和gcc。我有我自己的幻数的二进制文件。当我读取文件时,幻数不一样。溪流接缝是正确的。std :: fstream似乎以不同的大小读取
书写神奇的数字:
std::fstream chfile;
chfile.open(filename.c_str(), std::fstream::binary | std::fstream::out);
if (chfile.good())
{
chfile << (unsigned char)0x02 << (unsigned char)0x46 << (unsigned char)0x8A << (unsigned char)0xCE;
// other input
chfile.close();
}
阅读神奇的数字:
std::fstream chfile;
chfile.open(filename.c_str(), std::fstream::binary | std::fstream::in);
if (chfile.good())
{
unsigned char a,b,c,d;
chfile >> a;
chfile >> b;
chfile >> c;
chfile >> d;
printlnn("header must : " << (int)0x02 << ' ' << (int)0x46 << ' ' << (int)0x8A << ' ' << (int)0xCE); // macro for debugging output
printlnn("header read : " << (int)a << ' ' << (int)b << ' ' << (int)c << ' ' << (int)d);
chfile.close();
}
当我使用02 46 8A CE为神奇的数字,这是正常(如输出说):
header must : 2 70 138 206
header read : 2 70 138 206
但是当我使用EA 50 0C C5时,输出是:
header must : 234 80 12 197
header read : 234 80 197 1
最后1是下一个输入的合法值。那么,为什么他们不同,我如何解决这个问题?
在STL中没有'fstream',加上我非常怀疑你正在使用它。 – Fanael 2012-02-27 17:50:23
@Fanael我写过STL的fstream吗?在标题中它说std :: fstream。如果是这样,是否有人可以更改标题? – JoshuaBehrens 2012-03-30 00:05:32
是的,你做到了。请参阅[修订历史记录](http://stackoverflow.com/posts/9469361/revisions)。 – Fanael 2012-04-04 11:44:15