【问题标题】:Reading in input file with strings and doubles separated by commas and multiple lines. C++用逗号和多行分隔的字符串和双精度值读取输入文件。 C++
【发布时间】:2014-03-14 18:18:26
【问题描述】:

如何读取包含由多行逗号分隔的字符串和双精度数的输入文件。我想将字符串保存在多维数组中,例如 char Teams[5][40] 例如,该文件类似于:

Team1,0.80,0.30
Team1,0.30,0.20
Team1,0.20,0.70
Team1,0.70,0.80
Team1,0.90,0.20

我正在考虑使用 strtok,但我不确定如何迭代所有内容和所有行,同时将双打分配给每行上的字符串。有任何想法吗?谢谢!

【问题讨论】:

    标签: arrays input char double strtok


    【解决方案1】:

    您可以使用 getline 的第三个参数来设置除 '\n' 以外的分隔符。您将使用以下代码的数组版本:

    string temp;
    while(std::getline(file,line))
    {
        std::getline(ss,temp,','); // read first value into temp and move the stream to next value
    
        string team(temp); 
        int a,b,c;
    
        ss >> a; // read the stream into a
        std::getline(ss,temp,',');  // move to next value
        ss >> b;  // read into b
        std::getline(ss,temp,','); // move to next value
        ss >> c; // read into c
    }
    

    【讨论】:

    • 感谢拉撒路的帮助!你如何也将每行双数放入他们自己的二维字符数组中
    • 我也可以多次使用getline吗?
    • 将双数存储为字符串是一种不好的做法。你为什么要那样做。是的,您可以多次使用 getline,如示例中所示。
    • 我需要洗牌。
    猜你喜欢
    • 1970-01-01
    • 2016-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多