如何读取具有由多行逗号分隔的字符串和双精度的输入文件。我想把字符串保存在像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 ,但我不确定如何在所有行和所有行中进行迭代,同时保持每行上分配给其字符串的双精度数。有任何想法吗?谢谢!
答案 0 :(得分:2)
您可以使用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
}