csv文件其实就是文本文件,每行字段用逗号分隔。
代码
- #include <iostream>
- #include <string>
- #include <vector>
- #include <fstream>
- #include <sstream>
-
- using namespace std;
-
-
- int main()
- {
-
- ofstream outFile;
- outFile.open("data.csv", ios::out);
- outFile << "name" << ‘,‘ << "age" << ‘,‘ << "hobby" << endl;
- outFile << "Mike" << ‘,‘ << 18 << ‘,‘ << "paiting" << endl;
- outFile << "Tom" << ‘,‘ << 25 << ‘,‘ << "football" << endl;
- outFile << "Jack" << ‘,‘ << 21 << ‘,‘ << "music" << endl;
- outFile.close();
-
-
- ifstream inFile("data.csv", ios::in);
- string lineStr;
- vector<vector<string>> strArray;
- while (getline(inFile, lineStr))
- {
-
- cout << lineStr << endl;
-
- stringstream ss(lineStr);
- string str;
- vector<string> lineArray;
-
- while (getline(ss, str, ‘,‘))
- lineArray.push_back(str);
- strArray.push_back(lineArray);
- }
-
- getchar();
- return 0;
- }
结果

http://blog.csdn.net/u012234115/article/details/64465398
C/C++读写csv文件(用getline探测逗号分隔符)
原文:http://www.cnblogs.com/findumars/p/7252854.html