1

私は C++ の初心者です。次のような内容のテキスト ファイルがあります。

Systemname    localtesthost
SystemIp      X.X.X.X
Systemowner   root
...

ここで、「Systemname」の値、つまり「localtesthost」を抽出したいと思います。

そして、「Systemname localtesthost」を含む行を抽出できますが、文字列「Systemname」を抽出する方法がわかりません。

以下は私のプログラムです。

const char* configInfoFile = "config_info";//the text file name
ifstream ifs(configInfoFile);
    string line;
    while(getline(ifs,line)) {
        if(line.length() > 0){
            int index = line.find("SystemName");
            if (index != -1)
            {

                                .
                                .

            }

        } 
}

文字列を抽出する方法を教えてもらえます"localtesthost"か?

どうもありがとう!

4

1 に答える 1

5

アプローチは次のとおりです。

  1. 読み込んだ行を使用して を作成しstd::istringstreamます。
  2. std::stringキーと値の 2 つのオブジェクトをストリームから読み取る
  3. キーが探しているものと一致する場合、値には必要なものが含まれている必要があります。
于 2011-03-10T14:55:01.313 に答える