0

所以我正在编写一个用户文件创建,到目前为止我已经完成了,但我被困在如何在文本中获取特定行并在程序中使用该变量。

    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 中完成。

4

1 回答 1

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;
}
于 2013-10-12T21:19:08.150 回答