c++ - C++处理文本文件——从文本文件中提取某个字符串

标签 c++ string getline substr strtok

我是 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"

非常感谢!

最佳答案

这里有一个方法:

  1. 使用您刚刚读入的行创建一个 std::istringstream
  2. 从流中读取两个 std::string 对象,key 和 value
  3. 如果键匹配您要查找的内容,则值应包含您需要的内容。

关于c++ - C++处理文本文件——从文本文件中提取某个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5261409/

相关文章:

c - 保持前导零 C

c - 输入返回正确的字符串但不运行函数

c++ - 固定增长的STL容器

c++ - 如何修复将文字字符串附加到 C 字符串的错误?

swift - 如何从格式化字符串中填充结构对象?

c++ - 读取一行输入,包括以空格分隔的字符串,并使用 getline 将它们显示为一个完整的行

c++ - 在 UDP 套接字中使用 Connect()、send()、recv 时出现问题

c++ - GLEW 未在 MSYS2 上定位 OpenGL 函数

c++ - std::deque<char> 上的简单插入-删除-插入给出了奇怪的结果

javascript - 删除字符串中所有出现的文本