C++;fstream-telling行为 我正在研究一个C++应用程序(32位)。当我使用fstream读取一个文件并读取四个字节两次时,我从tellg中得到了令人困惑的值。我期待着0,4和8 std::fstream file; file.open(filename, std::ios::in , std::ios::binary); if ( !file.is_open()) { throw exception("Error opening file for reading"); } int pos = file.tellg(); // pos is 0 boost::int32_t usedBlocks; int size = sizeof (usedBlocks); file.read(reinterpret_cast<char*>(&usedBlocks),sizeof(usedBlocks)); pos = file.tellg(); //pos is 3588 //Read reserved size file.read(reinterpret_cast<char*>(&reservedSize),sizeof(reservedSize)); pos = file.tellg(); //pos is 3592 std::fstream文件; open(文件名,std::ios::in,std::ios::binary); 如果(!file.is_open()) { 抛出异常(“打开文件读取时出错”); } int pos=file.tellg();//pos为0 boost::int32_t usedblock; int size=sizeof(usedblock); read(reinterpret_cast(&usedblock),sizeof(usedblock)); pos=file.tellg()//电话号码是3588 //读取保留大小 read(reinterpret_cast(&reservedSize),sizeof(reservedSize)); pos=file.tellg()//电话号码是3592
为什么会发生这种情况 我已将代码更改为使用fopen、fread和ftell,然后pos值为0、4和8C++;fstream-telling行为 我正在研究一个C++应用程序(32位)。当我使用fstream读取一个文件并读取四个字节两次时,我从tellg中得到了令人困惑的值。我期待着0,4和8 std::fstream file; file.open(filename, std::ios::in , std::ios::binary); if ( !file.is_open()) { throw exception("Error opening file for reading"); } int pos = file.tellg(); // pos is 0 boost::int32_t usedBlocks; int size = sizeof (usedBlocks); file.read(reinterpret_cast<char*>(&usedBlocks),sizeof(usedBlocks)); pos = file.tellg(); //pos is 3588 //Read reserved size file.read(reinterpret_cast<char*>(&reservedSize),sizeof(reservedSize)); pos = file.tellg(); //pos is 3592 std::fstream文件; open(文件名,std::ios::in,std::ios::binary); 如果(!file.is_open()) { 抛出异常(“打开文件读取时出错”); } int pos=file.tellg();//pos为0 boost::int32_t usedblock; int size=sizeof(usedblock); read(reinterpret_cast(&usedblock),sizeof(usedblock)); pos=file.tellg()//电话号码是3588 //读取保留大小 read(reinterpret_cast(&reservedSize),sizeof(reservedSize)); pos=file.tellg()//电话号码是3592,c++,fstream,C++,Fstream,为什么会发生这种情况 我已将代码更改为使用fopen、fread和ftell,然后pos值为0、4和8 std::fstream file; file.open(filename, std::ios::in , std::ios::binary); if ( !file.is_open()) { throw exception("Error opening file for reading"); } int pos = file.tellg(); // pos is 0 boost::
std::fstream file;
file.open(filename, std::ios::in , std::ios::binary);
if ( !file.is_open())
{
throw exception("Error opening file for reading");
}
int pos = file.tellg(); // pos is 0
boost::int32_t usedBlocks;
int size = sizeof (usedBlocks);
file.read(reinterpret_cast<char*>(&usedBlocks),sizeof(usedBlocks));
pos = file.tellg(); //pos is 3588
//Read reserved size
file.read(reinterpret_cast<char*>(&reservedSize),sizeof(reservedSize));
pos = file.tellg(); //pos is 3592
usedBlocks
是一个boost::int32
<代码>boost::int32实际上是一个int
,而不是一个结构。即使将它们更改为int也会得到相同的结果。如果您在调试器中查看pos
的值,由于优化,它们可能是错误的
尝试将
pos
的值打印到标准输出中。reservedSize的类型是什么?我不熟悉open
功能的过载。我很确定这不是标准的。不管是什么情况,这都不是您要放置的ios::binary
。您需要在第二个参数中按位或与ios::in
一起使用。i、 e.file.open(文件名,std::ios::in | std::ios::binary)代码>--第三个参数用于其他参数(文件保护规范),您甚至不应该使用它。谢谢。我把逗号放错了。我应该使用|我确实从调试器中查看了值。谢谢你指出它可能是错的。