c++读取txt文件,有三行。每一行分别存入相应的指定vector中
c++,读取data.txt,文件里面含有三行数字,每行数字个数不限,怎么将他们分别读出存入到的三个vector,v1,v2,v3中?...
c++,读取data.txt,文件里面含有三行数字,每行数字个数不限,怎么将他们分别读出存入到的三个vector,v1,v2,v3中?
展开
1个回答
展开全部
#include <fstream>
#include <vector>
#include <sstream>
#include <cstddef>
using namespace std;
int main ()
{
ifstream InFile;
InFile.open ("data.txt");
if (InFile)
{
string line;
int number;
vector<vector<int> > v(3);
for (size_t i = 0; i != 3; ++i)
{
getline (InFile, line);
istringstream iss(line);
while (iss >> number)
v[i].push_back (number);
}
InFile.close ();
InFile.clear ();
}
else
throw runtime_error ("打开文件出错!");
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询