【问题标题】:how to get specific data from a txt file in c++ [duplicate]如何从c ++中的txt文件中获取特定数据[重复]
【发布时间】:2021-11-30 17:13:23
【问题描述】:

这是我的第一份出版物,我有一个问题- 我有一个 .txt 文件,我想用“,”分隔。 例子: 示例 1、示例 2、示例 3、示例 4 我想获取 example1 并存储在一个数组中,以此类推,例如 example2、example3、example4。 字符串 [0] = 示例 1 字符串 [1] = 示例 2 等等……

【问题讨论】:

    标签: c++ txt


    【解决方案1】:

    getline 默认情况下会按结束行字符分割输入,但可以用于任何字符,包括 ',' - 参见示例(使用字符串流):

    std::string input = "word1,word2,word3";
    std::stringstream sstream(input);
    
    std::string word;
    while(std::getline(sstream, word, ',')) {
        std::cout << word << '\n';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-05
      相关资源
      最近更新 更多