C ++:无法修改文件中的某个记录

时间:2016-02-01 13:09:20

标签: c++ file-io file-handling

我正在尝试创建一个登录表单,并且我已经选择在用户登录后修改用户的电子邮件ID。为此,我编写了以下代码。

char newmail[50];
                         cout<<"Enter New Email Id\n";
                         cin>>newmail;
                         fstream f2;
                         f2.open("db.txt" ,  ios::out |ios::ate| ios::binary);
                         strcpy(u.email,newmail);
                         f2.seekp(pos,ios::beg);
                         cout<<f2.tellp()<<endl;
                         f2.write((char*)&u, sizeof(user));
                         f2.close();
                         cout<<"Details edited sucessfully\n";
              break;

问题在于:我登录。选择修改电子邮件ID的选项。输入新的。没有错误,细节编辑成功。然后,当我去检查用户列表(为此编写代码,完美工作)时,所有用户都无效,即他们的数据为空但仍占用空间,并且只有一条记录包含所有数据,是的是电子邮件修改了id。当我使用f2.seekp(-sizeof(u),ios::cur);专门将指针设置在当前记录时,我无法弄清楚为什么所有数据都无效。 我已经交叉检查指针的位置,光标就在它的位置。 整个代码是这样的: -

    #include<iostream>
#include<fstream>
#include<string.h>
#include<stdlib.h>

using namespace std;

class user
{public:
    char username[50];
    char password[50];
    char email[50];


    int reg()
    {
        cout<<"REGISTRATION FORM\n";
        cout<<"Enter User name\n";
        cin>>username;
        cout<<"Enter Email\n";
        cin>>email;
        cout<<"Enter Password\n";
        cin>>password;
    }
    int putdata()
    {
        cout<<"Username   "<<username<<"         "<<"Email ID  "<<email<<endl;
    }

    char* retuser()
    {
       return username;
    }
    char* retpass()
    {

        return password;
    }
    char* retmail()
    {
        return email;
    }


};


int main()
{int a=sizeof(user);
    char ans;
    int ch;
    fstream f;
    user u;
    do
    {
        cout<<"MAIN MENU\n";
        cout<<"ENTER 1 FOR NEW REGISTRATION\n";
        cout<<"ENTER 2 TO LOGIN\n";
        cout<<"ENTER 3 FOR MEMBER LIST\n";
        cin>>ch;

        if(ch==1)
        {
           u.reg();
           f.open("db.txt", ios::app|ios::out|ios::binary);
            f.write((char*)&u, sizeof(user));
            f.close();
            cout<<"Registration successful\n";
        }
        if(ch==2)
        {
             char uname[50];
             char pass[50];
             int flag=0;
             cout<<"LOGIN\n";
             cout<<"Enter Username and Password\n";
             cin>>uname>>pass;
             f.open("db.txt", ios::in | ios::binary);
             f.read((char*)&u, sizeof(user));
             while(f)
             {
                 if(strcmp(u.retuser(),uname)==0)
                 {
                     if(strcmp(u.retpass(),pass)==0)
                     {
                         flag=1;
                         cout<<"Login Successful\n";
                         cout<<"USERNAME : -"<<u.retuser()<<endl;
                         cout<<"EMAIL ID : -"<<u.retmail()<<endl;
                         int pos;
                         pos=f.tellg();
                         pos=pos-a;
                         int i;
                         cout<<"Press 1 to modify email id, 2 to exit\n";
                         cin>>i;
                         if(i==1)
                         {
                             char newmail[50];
                             cout<<"Enter New Email Id\n";
                             cin>>newmail;
                             fstream f2;
                             f2.open("db.txt" ,  ios::out |ios::ate| ios::binary);
                             strcpy(u.email,newmail);
                             f2.seekp(pos,ios::beg);
                             cout<<f2.tellp()<<endl;
                             f2.write((char*)&u, sizeof(user));
                             f2.close();
                             cout<<"Details edited sucessfully\n";
                             break;
                         }
                         else if(i==2)
                         {
                             exit(0);
                         }
                     }
                 }
                 f.read((char*)&u, sizeof(user));
             }
             f.close();
              if(flag==0)
            {
                cout<<"Wrong username or password\n";
            }
        }
        if(ch==3)
        {
            cout<<"MEMBER LIST\n";

            f.open("db.txt", ios::in | ios::binary );
            f.read((char*)&u, sizeof(user));
            while(f)
            {
                u.putdata();
                f.read((char*)&u, sizeof(u));
            }
            f.close();

        }

        cout<<"Enter y to go to main menu\n";
        cin>>ans;
    }while(ans=='y' || ans=='Y');
}

1 个答案:

答案 0 :(得分:0)

为了能够在正确的位置修改文件,您应该打开文件(一次)进行输入和输出。我将只向您展示管理邮件地址更改的部分代码,即使您应该改进代码并在许多其他部分添加一些用户输入检查。

    if(ch==2) // I'd prefer a switch() here
    {
        char uname[50];
        char pass[50];
        int flag=0;
        cout << "LOGIN\n";
        cout << "Enter Username and Password\n";
        cin >> uname >> pass;
        fstream f("db.txt", ios::in | ios::out| ios::binary);
        if ( f.bad() ) {
            cout << "Error, unable to open users database file.";
            exit(1);
        }
        // check the position BEFORE reading the user
        int pos = f.tellg();
        while( f.read((char*)&u, sizeof(user)) ) {
            if( strcmp(u.retuser(),uname) == 0  
                    &&  strcmp(u.retpass(),pass) == 0 ) {
                flag = 1;
                cout << "Login Successful\n";
                cout << "USERNAME : -"<<u.retuser()<<endl;
                cout << "EMAIL ID : -"<<u.retmail()<<endl;
                int i;
                cout << "Press 1 to modify email id, 2 to exit\n";
                cin >> i;
                if( i == 1 ) {
                    char newmail[50];
                    cout << "Enter New Email Id\n";
                    cin >> newmail;
                    strcpy(u.email,newmail);
                    f.seekp(pos,ios::beg);
                    f.write((char*)&u, sizeof(user));
                    cout << "Details edited sucessfully\n";
                    break;
                } else if ( i == 2 ) {
                    exit(0);
                }
            }
            pos=f.tellg();
        }
        f.close();
        if ( flag == 0 ) {
            cout << "Wrong username or password\n";
        }
    }