readingcsv.cpp
来自「read from .csv files」· C++ 代码 · 共 52 行
CPP
52 行
#include "stdafx.h"
#include <algorithm>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;
void readCSV(istream &input, vector<vector<string>> &output)
{
string csvLine;
// read every line from the stream
while(getline(input, csvLine))
{
istringstream csvStream(csvLine);
vector<string> csvColumn;
string csvElement;
// read every element from the line that is seperated by commas
// and put it into the vector or strings
while(getline(csvStream, csvElement, ','))
{
csvColumn.push_back(csvElement);
}
output.push_back(csvColumn);
}
}
int main()
{
fstream file("TOPSIS-in.csv", fstream::in);
if(!file.is_open())
{
cout << "File not found!\n";
return 1;
}
// typedef to save typing for the following object
typedef vector< vector<string> > csvVector;
csvVector csvData;
readCSV(file, csvData);
// print out read data to prove reading worked
for(csvVector::iterator i = csvData.begin(); i != csvData.end(); ++i)
{
for(vector<string>::iterator j = i->begin(); j != i->end(); ++j)
{
cout << *j << ", ";
}
cout << "\n";
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?