簡體   English   中英

如何基於C ++中提供的信息使用getline()讀取文件中的下一個單詞?

[英]How to read the next word using getline() in a file based on an information given in C++?

我是C ++的新手,我需要文件操作方面的幫助。 我正在使用getline()讀取文件,當我收到冒號或分號等時,我想將下一個單詞存儲為類的元素。 我使用getline()從文件中讀取並將特定值存儲為類元素:

void read(string f){
    string s;
    ifstream fin;
    fin.open(f.c_str());

    configFile c;

    while(fin){
        getline(fin, s, ' '); //reading from file, terminates when finds space
        s = small(s); // converting to lower case
        if (s = "version/phase:"){
            c.ver = getline() // want to store the next word in c.ver
        }
    }
}

您可以嘗試以下操作。

void read(string f){
string s;
ifstream fin;
fin.open(f.c_str());

configFile c;

while(fin>>s){
    //getline(fin, s, ' '); //reading from file, terminates when finds space
    s = small(s); // converting to lower case
    if (s == "version/phase:"){
        fin>>s;
        c.ver = s;// getline() // want to store the next word in c.ver
    }
  }
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM