论坛: 编程破解 标题: 我这样在文件中修改一个参数的值,为什么修改不成? 复制本贴地址    
作者: zhoen889 [zhoen889]    论坛用户   登录
这段程序如下:
bool BOOKTICKET::modifysercet(char *id,char *password)
{
char pa[18];
char pas[18];
fstream fp;
fp.open("user.dat",ios::in|ios::out|ios::binary);

while(fp.read((char *)&user,sizeof(user)))
{
if((check_user(id,password))==true)
{
cout<<"Please input the new sercet:";
cin>>pa;
cout<<"Please input the new sercet again:";
cin>>pas;
for( ; ; )
{
if((strcmp(pa,pas))==0)
{
strcpy(user.user_id,id);
strcpy(user.user_password,pa);
fp.write((char *)&user,sizeof(user));
fp.close();
return true;
break;
}
else
{
cout<<"Input the same sercet!"<<endl;
cout<<"Please input the new sercet:";
                cin>>pa;
        cout<<"Please input the new sercet again:";
        cin>>pas;
}
}
}
}
return false;
}
在这段程序中,我想修改user.password的值。我先找到记录,然后修改,可是发觉总是修改不了,请问是我的程序有问题,还是编译有问题?
多谢!!!

地主 发表时间: 05-03-15 21:44

回复: 286 [unique]   版主   登录
养成如下好习惯。
1 fp.open("user.dat",ios::in|ios::out|ios::binary);//读其返回值,这个读取是否正确。
2 while(fp.read((char *)&user,sizeof(user)))//user是什么类型的?在哪里定义了?
3 fp.write((char *)&user,sizeof(user));//此操作之前,是否需要把文件指针移到最头处。否则有事能把数据续到后面。
4 然后修改,可是发觉总是修改不了,//说明清楚,是怎么修改不了,是改了没结果,还是改后结果和想象中的不一致,还是别的原因。


B1层 发表时间: 05-03-16 09:16

回复: zhoen889 [zhoen889]   论坛用户   登录
我是这样定义的:
struct User{
    char user_id[10];
    char user_password[18];
};
struct User  user;
因为user是在类中定义的,所以这里就不用考虑了( 忘记写明:))
至于fp.open("user.dat",ios::in|ios::out|ios::binary);我想改为fp.open("user.dat",ios::out|ios::binary);比较好
还有修改的问题,已经解决。
多谢斑竹的提醒!

B2层 发表时间: 05-03-16 22:52

论坛: 编程破解

20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon

粤ICP备05087286号