简体   繁体   English

C++ 二进制输入/输出文件读/写访问破坏

[英]C++ Binary Input/output file Read/Write Access voilation

i trying to read pass string from a file and then comparing it if the pass is correct but read/write access voilation occurs.我试图从文件中读取传递字符串,然后比较它是否传递正确但发生读/写访问冲突。 if i compare it with account number int then works fine but hwen compare with strig then this voilation error comes and the other thing is that afer that i cannot use that account instantiate stored in binary file even if i try to read that istantiate with int acoount number.如果我将其与帐号 int 进行比较,则工作正常,但 hwen 与 strig 进行比较,则出现此错误,另一件事是即使我尝试使用 int acount 读取该实例,我也无法使用存储在二进制文件中的该帐户实例数字。 But when i create new instatiate and store it in file then the new insatitae is available to comapre with int until it is not compared with string但是当我创建新的 instatiate 并将其存储在文件中时,新的 insatitae 可用于与 int 进行比较,直到它不与字符串进行比较

here is my class**
    
class account {
    std::string name , pass;
    int acno , balance;
    public:
        int getAcno()
        {
            return acno;
        }
        std::string getPass()
        {
            return pass;
        }
            std::string getName()
            {
                return name;
            }
            int getBalance()
            {
                return balance;
            }
            void createAccount()
            {
                std::cout << "\n\tEnter the account number : ";
                std::cin >> acno;
                std::cin.ignore(32767, '\n');
                std::cout << "\n\tEnter the account pass : ";
                std::cin >> pass;
                std::cin.ignore(32767, '\n');
                std::cout << "\n\tEnter the name of account holder : ";
                std::getline(std::cin , name);
                std::cin.ignore(32767, '\n');
                std::cout << "\n\tEnter the current balance : ";
                std::cin >> balance;
                std::cin.ignore(32767, '\n');
                
            }
            void showAccount()
            {
                std::cout << "\n\n\tAccount number : " << acno;
                std::cout << "\n\tAccount holder name : " << name;
                std::cout << "\n\tBalance : " << balance;
            }
    
    };
    '''
        here is my access function 
        void login()
        {
            std::ifstream infile{ "user.dat" , std::ios::binary | std::ios::in};
            if (!infile)
            {
                std::cout << "\n\tError! cannot open the file";
                return;
            }
        
            int acno;
            std::string pass = "jam";
            std::cout << "\n\n\tEnter the account number: ";
            std::cin >> acno;
            std::cout << "pass";
            std::cin >> pass;
            account new_account;
            bool flag = false;
            while (infile.read((char*)&new_account, sizeof(new_account)))
            {
                if (new_account.getPass() == pass)
                {
                    new_account.showAccount();
                    flag = true;
                    break;
                }
            }
            if (flag == false)
            {
                std::cout << "\n\tSorry! Account number not found!";
                    }
                }

As in the comments mentioned you should not write your objects like they are stored in RAM especially if they have some "reference-like" semantics like the subobject of your account std::string.正如在评论中提到的那样,您不应该像存储在 RAM 中那样编写对象,特别是如果它们具有一些“类似引用”的语义,例如您的帐户 std::string 的子对象。

What you write in this case is typically a pointer and a length.在这种情况下你写的通常是一个指针和一个长度。 The problem is, when you read the data again, the loaded pointer points to "garbage".问题是,当您再次读取数据时,加载的指针指向“垃圾”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM