使用现有文件 c++ 登录
Login with existing file c++
>我有一个基本的登录系统,它将用户输入的用户名和密码保存到文件中,用户可以通过读取文件的系统登录并检查是否存在现有的用户名和密码。但是当我关闭程序并再次运行它时,它会覆盖文件中的现有文本。我需要程序运行,其中它不会删除文件中的文本,而是继续到停止的点。另外,我不知道如何检查该文件是否已存在。
int main(){
string username;
string password;
string xuser;
string xpass;
ifstream xFile;
ofstream cFile;
xFile.open("login.txt");
cFile.open("login.txt");
// if(there are existing username and password in the file){
// the file will not be overwritten and continues off to a new line
// }
cout<<"Register"<<endl;
cout<<"Username: ";
cin>>username;
cout<<"Password: ";
cin>>password;
cFile << username << endl;
cFile << password << endl;
system("CLS");
cout<<"Login"<<endl;
cout<<"Username: ";
cin>>username;
cout<<"Password: ";
cin>>password;
xFile >> xuser;
xFile >> xpass;
if(username == xuser){
cout<<"Username correct"<<endl;
}
if(password == xpass){
cout<<"Password correct"<<endl;
}
xFile.close();
cFile.close();
return 0;
}
问题
使用 std::ofstream 写入文件时,默认行为是覆盖任何现有内容。
溶液
告诉std::ofstream
您希望它追加到文件中,而不是在打开流时通过传递std::ofstream::app
来覆盖所有内容。
替换以下行:
cFile.open("login.txt");
跟
cFile.open("login.txt", std::fstream::out | std::fstream::app);
注意
您必须与std::fstream::app
一起传递std::fstream::out
。这是因为传递您自己的模式将覆盖任何默认模式,包括 std::fstream::out
。
相关文章:
- 密码登录程序将永远循环并显示不正确的结果
- 如何检查cURL是否成功登录?c ++
- 在 Tizen 中登录特定文件?
- 如何在 c++ 中将密码和用户名保存到 .txt 文件中.如果用户尝试登录,我仍然希望能够检索它们
- Win7 C++ - 从以用户身份登录的服务启动可执行文件的问题
- 在主窗口之前显示登录对话框并隐藏主窗口按钮
- 如何使用 libcurl 登录本网站?(C++)
- 如何使用 Apache C 模块登录到文件
- 用户注销/登录后,创建托盘图标时出现罕见错误
- 将正在运行的程序的用户名与登录到Windows的用户名进行比较
- 使用二进制文件的用户名和登录系统
- C++登录面板以查看 cout
- 从C++ Windows 服务中获取当前登录的用户名
- 是否有任何 C 函数或 API 来获取当前登录用户下运行的进程列表
- 不会在登录名中读取C++我的文件
- 尝试制作登录程序
- QT MySql 数据库检查登录名和密码以登录
- 与远程登录客户端协商回声
- 如何使用 cURL 防止在解析服务器中进行多次登录
- 如何从远程计算机中获取当前登录的用户名