程序不能从二进制文件中读取

Program not reading from Binary file

本文关键字:读取 二进制文件 不能 程序      更新时间:2023-10-16

我正在尝试为我在学校做的一个项目创建一个测试程序。我的项目的一个重要方面是有一个登录系统。在int main()中,我有一个菜单,根据用户的选择调用login();createAccount();login();进一步调用loginToken();生成int authToken,其值为01。它调用loginAuth(),然后检查authToken的值并对登录进行身份验证。在int main();中,当我调用createAccount()时,它将newUsernamenewPassword的值写入二进制文件accounts.bin。然而,当我调用login()时,检查loginEntry是否打开的if条件显示它是关闭的,即使我刚刚在前一行打开它。

我很感激你的帮助,因为这件事已经困扰我好几天了。
#include<iostream>
#include<string.h>
#include<fstream>
#include <limits>
using namespace std;
class Account{
  private:
    char enteredUsername[20];
    char enteredPassword[20];
    int authToken;
    char newUsername[20]; 
    char newPassword[20];
    ofstream loginDetails;
    ifstream loginEntry;
  public:   
    void login(){ // to enter username or password
        cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');  //to clear cin buffer
        loginEntry.open("account.bin", ios::in|ios::binary); // to read newUsername and newPassword
            if (!loginDetails.is_open()) {  //to check whether file is open or not
            cout<<"File not open";
            }
            else {
                loginEntry.read(newUsername,sizeof(newUsername));
                loginEntry.read(newPassword,sizeof(newPassword));
            }
          loginEntry.close();
        cout<<"nEnter Username: ";
        cin.getline(enteredUsername, sizeof(enteredUsername));
        cout<<"nEnter Password: ";
        cin.getline(enteredPassword, sizeof(enteredPassword));
        loginToken(); 
    }
    void loginToken(){ // to generate login token if enteredUsername and enteredPassword match newUsername and newPassword in account.bin 
        if(strcmp(enteredUsername,"user")==0 && strcmp(enteredPassword,"admin")==0)
            authToken=1;
        else
            authToken=0;
        loginAuth(); // to check value of login token and allow or deny access
    }
    void loginAuth(){ 
        if(authToken==1)
            cout<<"Login succesfull!!";
        else
            cout<<"Login Failed. Returning to Menu";
        getchar();
    }
    void createAccount(){  // to create newUsername and newPassword which are saved in account.bin
        cin.ignore(numeric_limits<streamsize>::max(), 'n');    //to clear cin buffer
        cout<<"nEnter new Username: "; 
        cin.getline(newUsername,sizeof(newUsername));
        cout<<"nEnter new Password: ";
        cin.getline(newPassword,sizeof(newPassword)); 
        loginDetails.open("account.bin", ios::out|ios::binary); //writing in account.bin
            loginDetails.write(newUsername, sizeof(enteredUsername));
            loginDetails.write(newPassword, sizeof(enteredPassword));
        loginDetails.close();
        cout<<"nYour account has been created. Returning to Menu";
        getchar();
    }
}; 
int main(){
Account test;
int userChoice;
do{
    system("cls");
    cout<<"Welcome to xyz.com";
    cout<<"n*Press 1 to Loginn*Not a memeber? Press 2 to create an account and be awesome!!n*If you're tired, press 3 to leave "<<endl;  
    cin>>userChoice;
    switch(userChoice){
        case 1:{
            test.login();
            break;          
        }
        case 2:{
            test.createAccount();
            break;
        }
    }
}while(userChoice!=3);
cout<<"GoodBye!! :)";
return 0;
}

在login()中有:

loginEntry.open("account.bin", ios::in|ios::binary);

然后是

if (!loginDetails.is_open()) {

你打开了"entry",而不是"details",但是你正在检查"details",这就是为什么它说错误。

<标题>编辑

我发现有两个新的潜在问题。

在createAccount()中,第二个参数sizeof()返回大小为0,因为您的方法中没有使用enteredUsername和enteredPassword,因此没有向文件写入任何内容。你可能想用newUsername和newPassword来代替。

loginDetails.write(newUsername, sizeof(enteredUsername));
loginDetails.write(newPassword, sizeof(enteredPassword));

在loginToken()中,您使用像'user'这样的常量,而不是刚刚在login()中创建的名为newUsername的变量。此外,

loginEntry.read(newUsername,sizeof(newUsername)); //unused
loginEntry.read(newPassword,sizeof(newPassword)); //unused
if(strcmp(enteredUsername,"user")==0 && strcmp(enteredPassword,"admin")==0)

最后,您在代码中使用getLine,这意味着您可能会在输入的末尾读取n字符。

cin.getline(enteredUsername, sizeof(enteredUsername));