c++ - 输入到文本文件

标签 c++ input passwords

这是我的代码:

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    int would;
    string pass;
    cout << "Password Manager v.1" << endl << endl;
    cout << "What's the secret?" << endl;
    cin >> pass;
    if(pass == "youcantknowsorry"){
        cout << "Access granted." << endl << endl;
        cout << "Would you like to add a new password (1) or view your passwords? (2)" << endl;
        cin >> would;
        if(would == 1){
            ofstream myfile;
            myfile.open ("example.txt");
            myfile << "NewPassword" << endl; <--- HOW CAN I MAKE THAT INPUT?
            myfile.close();
        }
        if(would == 2){
            cout << "Your passwords will open in a text file.";
        }
    }
    return 0;
}

我正在尝试为自己编写一个密码管理器。我已经使用类似 cout 的方法成功创建、打开和写入文件。但是,我需要用户输入信息并将其保存在文件中。

最佳答案

让我们假设这只是关于读取输入和写入文件,而不是在纯文本文件中管理密码。 你有

int would;
cin >> would;

string pass;
cin >> pass;

因此,您已经知道如何读取用户的输入。 同样,您可以从用户那里读取密码,并将其流式传输到文件中:

string password;
cin >> password;
myfile << password << endl;

关于c++ - 输入到文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17419171/

相关文章:

javascript - 检测输入 (type=text) 元素中的文本是否超出 FireFox 中的边界

java - 读取存储在 Arraylist 中的文件数据

c++ - 将输入从 std::cin 转换为可运行代码 C++

通过 PowerShell 连接 MySQL 数据库,旧式密码

c++ - 从相机投影线

c++ - 使用 C++ void 函数操作 vector 数组

c# - 用于开发皮肤应用程序的最佳框架是什么?

c++ - 有一对 <string, string> 如何查找它是否属于 map<string, string> 中的某个对?

php更改密码脚本尝试了很多不同的方法都不起作用

python - 使用 python 存储密码