我的文件看起来像这样
0 0 5
1 10 20
2 10 10
3 15 3
4 20 30
5 25 5
我将其保存在矢量中。我不知道如何阅读它,以便我可以将每个组件保存在不同的向量中。
尝试解决方案:
if (inFile.is_open()) {
while ( inFile) {
getline (inFile,line);
cout << line << endl;
}
inFile.close();
}
答案 0 :(得分:1)
我认为这就是你想要做的事情:
ifstream fin("C:/file.txt");
if(!fin)
{
cout<<"Cannot Open File";
exit(EXIT_FAILURE);
}
vector<vector<int>> v;
int vector_length = 3;
int count = 0;
while(!fin.eof()) //Read till the end of file
{
v.push_back(vector<int>()); //Add vector for new line
int number;
for(int i=0; i<vector_length; i++)
{
fin>>number; //Read a number from the file
v[count].push_back(number); //Add the number to the vector
}
count++;
}
//Show the output to confirm that the file has been read correctly
for(int i=0; i<v.size(); i++)
{
for(int j=0; j<v[i].size(); j++)
{
cout<<v[i][j]<<"\t";
}
cout<<endl;
}