所以我正在编写一个用户文件创建,到目前为止,我仍然坚持如何在文本中获取特定行并在程序中使用该变量。
int Player::CheckAccount()
{
Network net;
bool fileFound = false;
string user = GetLoginUser();
string openDir = "./Characters/" + user + "/" + user + ".ini";
if (fileFound == false) {
ifstream openFile(openDir);
if (openFile.good()) {
util::Logger::Dbg("File found for user " + user);
openFile.open(openDir);
util::Logger::Dbg("User file " + user + " is ready to be checked");
fileFound = true;
} else {
util::Logger::Dbg("Could not find user file for " + user + " creating new character file" );
CreateAccount(GetLoginUser());
}
}
return net.GetFinalSize() == 1;
}
void Player::CreateAccount(string user)
{
string dir = "./Characters/" + user;
if(CreateDirectory(dir.c_str(), NULL)) {
util::Logger::Dbg("Created new user directory for " + user);
} else {
util::Logger::Dbg("Could not create new user directory for " + user);
}
string fileName = user + ".ini";
ofstream createUser;
string charDir = "./Characters/" + user + "/" + fileName;
createUser.open(charDir);
SetPlayerBanned(false);
SetInAppUsername(user);
createUser << "Username = " << GetLoginUser() << endl;
createUser << "Password = " << GetLoginPass() << endl;
createUser << "app Username = " << GetInAppUsername() << endl;
createUser << "Status = " << GetStatus() << endl;
createUser << "Bio = " << GetBio() << endl;
createUser << "Banned status = " << IsPlayerBanned() << endl;
createUser << "Avatar dir = " << GetAvatarDir() << endl;
createUser.close();
}
所以在checkaccount函数中,我希望能够从文本文档中提取变量,这将是一个bool banstatus。虽然我不想做的是必须使用外部库我希望能够直接从Windows做到。
答案 0 :(得分:0)
这就是我解析配置文件的方式:
int config::Parse(void)
{
std::ifstream cfile("file.conf");
if(! cfile.is_open()) Util::Error(ErrorNum::NoFileOpen, confile, __func__);
str line = "";
std::regex rxlognum("("Blah: ")(.*)"); // "Blah: " is what you want to name the variable
std::smatch rxm;
while(getline(cfile, line))
{
if(std::regex_match(line, rxm, rxlognum))
{
TheVariable = rxm[2];
break;
}
}
cfile.close();
return 0;
}