我是 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"
?
非常感谢!
最佳答案
这里有一个方法:
- 使用您刚刚读入的行创建一个
std::istringstream
。 - 从流中读取两个
std::string
对象,key 和 value - 如果键匹配您要查找的内容,则值应包含您需要的内容。
关于c++ - C++处理文本文件——从文本文件中提取某个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5261409/