简体   繁体   中英

C++ How do i get a specific line of a text file

So i am writing a user file creation and i have got so far but i am stuck on how i would get a specific line in a text and use that variable in the program.

    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();
}

So in the checkaccount function i want to be able to extract the variable from the text document which would be a bool banstatus. Although i what i do not want to do is have to use an external lib i want to be able to do it straight from windows.

This is how I parse my configuration files:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM