2011-03-10 52 views
1

我是C++的新学习者。 我有一个文本文件,其内容如下一样:通过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"

非常感谢!

回答

5

这里有一个办法:

  1. 使用刚读入的行创建std::istringstream
  2. 从流中读取的两个std::string对象,key和value
  3. 如果是匹配的,你是什么寻找,价值应该包含你所需要的。
+0

尼姆,感谢您的方法。这比我想象的简单得多。谢谢! – zhaojing 2011-03-11 08:37:40

+0

@赵靖,不客气! – Nim 2011-03-11 09:26:44

相关问题