使用外部文件中的变量进行视觉C++

visual C++ using a variable from an external file

本文关键字:视觉 C++ 变量 外部 文件      更新时间:2023-10-16

我是C++新手,但到目前为止我喜欢它。

最近,我一直在制作越来越好的控制台应用程序构建,就像登录一样,但是您会在同一会话中提供密码字符串和用户名字符串。

我正在尝试这样做,以便您打开应用程序并检查某个文件,如果它不存在,它将创建它并要求您给它一个值。我希望这能使我能够设置一个用户创建的密码和用户名,而不是几个已经有值但不是用户创建的字符串。

我的最佳版本的代码(它不是很好的代码(也不会发布所有这些代码,只是他们创建通行证的部分。

#include "stdafx.h"
#include "iostream"
#include "string"
#include "windows.h"
using namespace std;

int main() {
string user;
string pass;
string entry;
cout << "Make your usernamen";
cin >> user;
cout << "Make your passwordn";
cin >> pass;
return 0;
}

这段代码还有很多(大约 80 行(,但我觉得不需要大部分信息。

使用std::ifstream管理文件

std::ifstream f("path_to_file");
if (f.fail()) {
// we can't us the file (doesn't exist, incorrect permissions, etc.)
// instead, ask the user to enter their credentials
} else {
// read the file and extract necessary information
}