C++读取csv
#include<sstream>
#include<fstream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
std::ifstream infile("F:\\Qt\\untitled3\\data.csv");
string line;
vector<vector<double>>data;
while (std::getline(infile,line)) {
std::stringstream rows(line);
string datai;
vector<double> datarow;
while(std::getline(rows,datai,','))
{
datarow.push_back(atof(datai.c_str()));
}
data.push_back(datarow);
}
for(int i = 0;i<data.size();i++)
{
for(int j = 0;j< data[0].size();j++)
std::cout<<data[i][j]<<',';
std::cout<<endl;
}
return 0 ;
}
|