赫雷什我的代碼二進制文件不能多次使用?
#include <iostream>
#include <fstream>
using namespace std;
struct a
{
int x;
int b;
};
int main()
{
ifstream rfile("test.bin", ios::binary);
a ob;
//Reading from the file for the first time works fine.
rfile.read((char*)&ob, sizeof(ob));
while (rfile)
{
cout<<ob.x<<endl;
rfile.read((char*)&ob, sizeof(ob));
}
rfile.seekg(0, ios::beg);
cout<<"G:"<<rfile.tellg()<<endl; //Outputs -1
rfile.read((char*)&ob, sizeof(ob));
while (rfile)
{
cout<<ob.x<<endl;
rfile.read((char*)&ob, sizeof(ob));
}
return 0;
}
輸出是
3
1
G:-1
如在第一環路作品和第二環路dosent工作,因爲指針的位置是在-1即使使用seekg()之後。這是爲什麼發生?