这是我写的不完整的代码。
string line;
ifstream fp ("foo.txt");
if (fp.fail()){
printf("Error opening file %s\n", "foo.txt");
return EXIT_FAILURE;
}
unsigned int lineCounter(1);
while(getline(fp, line)){
if(lineCounter == 1){
lineCounter++;
} // to skip first line
else{
// find and extract numbers
}
}
foo.txt 文件如下所示。
x0,x1,y0,y1
142,310,0,959
299,467,0,959
456,639,0,959
628,796,0,959
基本上,数字是 x 和 y 坐标。我需要的只是以易于阅读的数据类型提取数字,并且能够像矩阵一样访问它们。它应该存储为 4 个容器,4 行具有 [142, 310, 0, 959], [299, 467, 0, 959]...等等。
我尝试了 find() 函数,但我不确定如何正确使用它来将它们放入数据类型中。
如何仅提取数字并将它们存储在可以移动并像数组一样访问它们的数据类型中?