程序不读取二进制文件
Program not reading from Binary file
我正在尝试为我在学校做的项目创建一个测试程序。
我的项目的一个重要方面是拥有一个登录系统。
在 int main()
中,我有一个根据用户选择调用 login();
或 createAccount();
的菜单。 login();
进一步调用 loginToken();
生成 int authToken
,值为 0
或 1
。它调用 loginAuth()
,后者又检查 authToken
的值并验证登录。
在 int main();
中,当我调用 createAccount()
时,它将值或 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;
}
在 login() 中你有:
loginEntry.open("account.bin", ios::in|ios::binary);
然后你有
if (!loginDetails.is_open()) {
您打开了 "entry",而不是 "details",但您正在检查 "details",这就是它显示错误的原因。
编辑
我发现了 2 个新的潜在问题。
在 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));
我正在尝试为我在学校做的项目创建一个测试程序。
我的项目的一个重要方面是拥有一个登录系统。
在 int main()
中,我有一个根据用户选择调用 login();
或 createAccount();
的菜单。 login();
进一步调用 loginToken();
生成 int authToken
,值为 0
或 1
。它调用 loginAuth()
,后者又检查 authToken
的值并验证登录。
在 int main();
中,当我调用 createAccount()
时,它将值或 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;
}
在 login() 中你有:
loginEntry.open("account.bin", ios::in|ios::binary);
然后你有
if (!loginDetails.is_open()) {
您打开了 "entry",而不是 "details",但您正在检查 "details",这就是它显示错误的原因。
编辑
我发现了 2 个新的潜在问题。
在 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));