C++读取csv文件
C++读取csv文件,采用文件流的方法,用到了getline函数(带有自动分隔功能)
#include <iostream> #include <fstream> #include <sstream> #include <string> #include <vector> using namespace std; //注意,命名空间 std int main() { ifstream infile("Test.csv"); string line; getline(infile, line); while (getline(infile, line)) { istringstream sin(line); vector<string> fields; string field; while (getline(sin, field, ',')) { fields.push_back(field); } string number = fields[0]; string project_name = fields[1]; } }