我必须读取一个结构如下的二进制文件:n为1个字节,n个字节,4个字节等
那是我的代码:
char length;
file >> length;
char c[ 64 ];
file.read( c, length );
c[ length ] = 0;
int ver;
file >> ver;
问题:ver始终为0(在约2500个文件上),而不应该为0。长度输出正确的值,c也是如此。并在调用read之后,tellg返回-1。
你说
n个字节,n个字节,4个字节等为1个字节
我建议使用:
char length;
file.read(&length, 1);
// Check the value of length and make sure you have enough space.
if ( length > 63 )
{
// Deal with error condition
}
char c[ 64 ];
file.read( c, length );
c[ length ] = 0;
// Use a type that is know to be 4 bytes wide.
// Use read() instead of formatted input.
int32_t ver;
file.read(reinterpret_cast<char*>(&ver), sizeof(ver));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句