Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 程序不从二进制文件读取_C++_Class_Binaryfiles - Fatal编程技术网

C++ 程序不从二进制文件读取

C++ 程序不从二进制文件读取,c++,class,binaryfiles,C++,Class,Binaryfiles,我正在尝试为我在学校做的一个项目创建一个测试程序。 我的项目的一个重要方面是有一个登录系统。 在int main中,我有一个调用login的菜单;或创建帐户;根据用户的选择。登录;进一步致电loginToken;生成值为0或1的int authToken。它调用loginAuth,loginAuth反过来检查authToken的值并对登录进行身份验证。 以int为主;当我调用createAccount时,它会将vales或newUsername和newPassword写入一个二进制文件accou

我正在尝试为我在学校做的一个项目创建一个测试程序。 我的项目的一个重要方面是有一个登录系统。 在int main中,我有一个调用login的菜单;或创建帐户;根据用户的选择。登录;进一步致电loginToken;生成值为0或1的int authToken。它调用loginAuth,loginAuth反过来检查authToken的值并对登录进行身份验证。 以int为主;当我调用createAccount时,它会将vales或newUsername和newPassword写入一个二进制文件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 Login\n*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;
}
在登录中,您有:

loginEntry.open("account.bin", ios::in|ios::binary);
然后你有

if (!loginDetails.is_open()) {
您已经打开了条目,而不是详细信息,但您正在检查详细信息,这就是它显示错误的原因

编辑 我看到了两个新的潜在问题

在CreateCount中,第二个参数sizeof返回的大小为0,因为方法中未使用enteredUsername和enteredPassword,因此不会向文件写入任何内容。您可能打算改用newUsername和newPassword

loginDetails.write(newUsername, sizeof(enteredUsername));
loginDetails.write(newPassword, sizeof(enteredPassword));
在loginToken中,您使用诸如“user”之类的常量,而不是刚刚在名为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));

哦,我的上帝!非常感谢你。但现在我有另一个问题。我通过createAccount;创建了一个帐户。但当我叫登入;要登录,它表示登录失败。有什么帮助吗?我将编辑这个问题accordingly@AhmadZafar-该撰稿人回答了您最初的问题。就这样,你现在应该接受它。改变问题从而使答案无效是非常烦人和不友好的。我认为在这种情况下没关系,因为我是唯一一个回答:啊。我真傻,在将文件实现到源代码之前,我用它来检查程序的运行情况。忘了把它换回来。非常感谢您的时间:我投票将此问题作为离题题结束,因为OP正在修改问题描述和源代码,而不仅仅是接受他/她的原始问题的有效答案。我将接受答案,我是否应该将此作为另一个问题问?