一个简单读取文件C++问题`
void check(int id , char pwd[])
{
Userinfo *uif;
ifstream ifile;
int flag=0;
ifile.open( "test.txt ");
if(ifile.fail())
{
cout < < "不能打开文件 ";
exit(1);
}
while(!ifile.eof())
{
ifile.read((char *)uif,sizeof(Userinfo));
if (id==uif-> id)
{
flag=1;
if(!strcmp(pwd,uif-> pwd))
{
cout < < "密码正确 ";
}
else
{
cout < < "密码不正确 ";
}
}
}
不知道错在哪里`
[解决办法]
ifile.open( "test.txt ", ios_base::binary);
[解决办法]
并且,记得写文件的时候也要用binary模式。
[解决办法]
你的文件是二進制數據,就用binary打開,是一般文本,就用文本模式打開。