C++ 使用getline提取信息并将其存储在c++;

C++ 使用getline提取信息并将其存储在c++;,c++,extract,getline,C++,Extract,Getline,这是我写的一段代码,它并不完整 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){ lineCount

这是我写的一段代码,它并不完整

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条线有[142310,0959]、[299467,0959]……等等

我尝试了find()函数,但我不确定如何正确使用它将它们放入数据类型


如何仅提取数字并将其存储在一个数据类型中,这样我就可以在其中移动并像数组一样访问它们?

要读取以逗号分隔的4个数字,请执行此操作

std::string line;
std::getline(file, line);

std::stringstream linestream(line);

int  a,b,c,d;
char sep; // For the comma

// This should work even if there are spaces in the file.
// The operator >> will drop leading white space
// Then read the next type
//      For int object will read an integer from the stream
//      For char object will read the next char (after dropping leading white space)
linestream >> a >> sep >> b >> sep >> c >> sep >> d;

基于@Martin的答案:

std::string line;
std::getline(file, line);

std::stringstream linestream(line);

int  a,b,c,d;
char sep; // For the comma

// This should work even if there are spaces in the file.
// The operator >> will drop leading white space
// Then read the next type
//      For int object will read an integer from the stream
//      For char object will read the next char (after dropping leading white space)
linestream >> a >> sep >> b >> sep >> c >> sep >> d;
我们如何将这四个值放入类似矩阵的数据结构中?首先,在具有适当范围和生命周期的地方声明:

std::vector< std::vector<int> > matrix; // to hold everything.
这和:?
{
    std::vector <int> vi;
    vi.push_back(a);
    vi.push_back(b);
    vi.push_back(c);
    vi.push_back(d);
    matrix.push_back(vi);
}
int item;
item = matrix[0][0] + matrix[1][1] + matrix[2][2]; // for example.