Blog reprinted from: http://blog.csdn.net/u012234115/article/details/64465398
C + + read and write CSV file, note the format can
#include <iostream>#include<string>#include<vector>#include<fstream>#include<sstream>using namespacestd; intMain () {//Write a fileOfstream OutFile; Outfile.open ("Data.csv"Ios:: out);//open mode to omitOutFile <<"name"<<','<<" Age"<<','<<"Hobby"<<Endl; OutFile<<"Mike"<<','<< -<<','<<"paiting"<<Endl; OutFile<<"Tom"<<','<< -<<','<<"Football"<<Endl; OutFile<<"Jack"<<','<< +<<','<<"Music"<<Endl; Outfile.close (); //Read the fileIfstream InFile ("Data.csv"Ios::inch); stringLinestr; Vector<vector<string>>Strarray; while(Getline (InFile, linestr)) {//print an entire line of stringcout << linestr <<Endl; //into a two-dimensional table structureStringStream SS (LINESTR); stringstr; Vector<string>Linearray; //separated by commas while(Getline (SS, str,',') ) Linearray.push_back (str); Strarray.push_back (Linearray); } getchar (); return 0; }
Output
Read/write CSV file