2013-10-12 44 views
0

因此,我正在編寫一個用戶文件創建,並且到目前爲止,但我堅持如何獲取文本中的特定行並在該行中使用該變量程序。C++如何獲取文本文件的特定行

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功能我希望能夠從中提取這將是一個布爾banstatus文本文檔的變量。雖然我不想做的是必須使用外部庫,我希望能夠從Windows直接做到這一點。

+0

你還沒說你怎麼會告訴你想讀特定的線路。它會是一個包含這樣一個字符串的行,它是否會列出這樣那樣的數字?一般來說,獲取特定行的唯一方法是從文件的開頭讀取行,直到找到所需的行。 – john

+0

那麼通過文本文件循環?直到我說第6行?也是它的一個(字符串布爾)就行我想這樣玩家禁止= 0或1 –

+0

正確的,如果你想第6行,閱讀前五個並扔掉它們,然後閱讀第六,做任何你想要的做。 – john

回答

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

上面的「繼續」基本上沒有效果。如果你只想要首次出現'Blah',那麼你可能需要'bre​​ak'而不是'continue'。 – scott

+0

謝謝你,我會嘗試並將其添加到我的代碼:) –

相關問題