程序不能从二进制文件中读取
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 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));
标题>相关文章:
- 正在读取二进制文件(is_open)
- 读取二进制文件并解释为整数
- 读取二进制文件直到结束 c++
- 如何在标准c ++中流式传输/读取二进制文件的中间部分并写入另一个文件?
- 读取二进制文件的惯用C++17标准方法是什么
- 从末尾读取二进制文件
- C++逐个字符读取二进制文件字符
- 读取二进制文件大小的短自上周以来停止工作
- 读取二进制文件时出现问题
- 读取二进制文件时如何输出?
- C++:如何在线程中读取二进制文件并将其写入另一个线程?
- 在C 中读取二进制文件会返回意外值
- 尝试读取二进制文件会清除文件本身
- C 读取二进制文件
- 通过C 读取二进制文件的问题
- ifstream读取二进制文件在发行模式下工作,但在调试模式下不使用
- 分段 追加和读取二进制文件时出错
- 使用C++中的MsgPack读取二进制文件
- 有没有一种优雅的方法来读取二进制文件并将其内容复制到向量<int>
- 如何读取二进制文件并在屏幕上打印 3?