//#include <opencv2\opencv.hpp>
#include <iostream>
#include <fstream>
#include <vector>
#include <sstream> // 用于读写存储在内存中的string对象
int main(void)
{
// 制作CSV文件
/* Name,age,height
* Tom,21,172.8
* John,25,189.5
*/
std::ofstream outFile;
outFile.open("./csvTest.csv", std::ios::out);
outFile << "Name" << "," << "age" << "," << "height" << std::endl;
outFile << "Tom" << "," << 21 << "," << 172.8 << std::endl;
outFile << "John" << "," << 25 << "," << 189.5 << std::endl;
outFile.close();
// 读取CSV文件
struct CSVDATA {
std::string name;
int age;
double height;
};
std::ifstream inFile("./csvTest.csv", std::ios::in);
std::string lineStr;
std::vector<struct CSVDATA> csvData;
std::getline(inFile, lineStr); // 跳过第一行(非数据区域)
while (std::getline(inFile, lineStr)) {
std::stringstream ss(lineStr); // string数据流化
std::string str;
struct CSVDATA csvdata;
std::getline(ss, str, ','); // 获取 Name
csvdata.name = str;
std::getline(ss, str, ','); // 获取 age
csvdata.age = std::stoi(str);
std::getline(ss, str, ','); // 获取 height
csvdata.height = std::stod(str);
csvData.push_back(csvdata);
}
// 显示读取的数据
for (int i = 0; i < csvData.size(); i++ ) {
std::cout << csvData[i].name << "," << csvData[i].age << "," << csvData[i].height << std::endl;
}
getchar();
return 0;
}
C++对csv文件的读写
最新推荐文章于 2024-07-10 17:21:51 发布