使用C++读取csv文件
0.标准(C++11)
1.代码结构

2.Data.h文件
/**
*@Author:FlowLiver
*@Time: 2021/2/23
*@From http://...
*@ForUse happy
*/
#ifndef CPPPRJ_DATA_H
#define CPPPRJ_DATA_H
#include <string>
#include <utility>
class Data {
private:
std::string _name;
std::string _age;
std::string _gender;
public:
const std::string &getName() const;
Data &setName(const std::string &name);
const std::string &getAge() const;
Data &setAge(const std::string &age);
const std::string &getGender() const;
Data &setGender(const std::string &gender);
void print_info();
~Data() = default;
Data() = default;
Data(std::string name, std::string age, std::string gender)
: _age(std::move(age)),
_gender(std::move(gender)),
_name(std::move(name)) {
}
};
#endif //CPPPRJ_DATA_H
3.Data.cpp文件
/**
*@Author:FlowLiver
*@Time: 2021/2/23
*@From http://...
*@ForUse happy
*/
#include "Data.h"
#include <iostream>
const std::string &Data::getGender() const {
return _gender;
}
const std::string &Data::getName() const {
return _name;
}
Data &Data::setGender(const std::string &gender) {
_gender = gender;
return *this;
}
Data &Data::setName(const std::string &name) {
_name = name;
return *this;
}
const std::string &Data::getAge() const {
return _age;
}
Data &Data::setAge(const std::string &age) {
_age = age;
return *this;
}
void Data::print_info() {
std::cout << "name is :" << _name
<< "\tage is :" << _age
<< "\tgender is :" << _gender
<< std::endl;
}
3.数据源文件
张三,22岁,男
李四,23岁,女
陈六,23岁,女
4.main.cpp文件
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include "Data.h"
using namespace std;
bool readFile(const string &, vector<Data> &, char, int);
int main(int argc, char **argv) {
vector<Data> dataArray;
//csv文件有3行,sum=3
if (!readFile("E:/files/codesrc/Clion/cppPrj/mx.csv", dataArray, ',', 3)) {
cout << "read File fail!" << endl;
}
for (auto &data : dataArray) {
data.print_info();
}
}
bool readFile(const string &str, vector<Data> &dataArray, char flag, int sum) {
ifstream inFile(str, ios::in);
string line;
if (!inFile.is_open()) {
cout << "open file fail......" << endl;
return false;
}
vector<string> data_str;
int count = 0;
while (getline(inFile, line) && count != sum) {
Data data;
data_str.clear();
stringstream ss(line);
string strIem;
while (getline(ss, strIem, flag)) {
data_str.emplace_back(strIem);
// cout<<strIem<<"******"<<endl;
}
cout << data_str[0] << "******" << endl;
data.setName(data_str[0])
.setAge(data_str[1])
.setGender(data_str[2]);
cout << data.getName() << "-------" << endl;
dataArray.emplace_back(data);
++count;
}
return true;
}
已完成
以下已废弃
1.代码1如下
//FileRead.h
//
// Created by hhlt on 2020/1/6.
//from file of ".csv" to array by C++
//
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#ifndef UNTITLED1_FILEREAD_H
#define UNTITLED1_FILEREAD_H
using namespace std;
class FileRead{
private:
vector<vector<string>> strArray;
string str="";
public:
FileRead(string temp) :str(temp){
}
bool read_file(){
ifstream inFile(str, ios::in);
string lineStr;
char flag=';';
if(inFile.fail()){
cout <<"Read files fail....."<<endl;
return false;
}
while (getline(inFile, lineStr)) {
stringstream ss(lineStr);
string str;
vector<string> lineArray;
// cut apart by flag
while (getline(ss, str, flag))
lineArray.push_back(str);
strArray.push_back(lineArray);
}
return true;
}
void show_str_array(int row,int col){
if(!read_file()){
cout<<"No array!"<<endl;
cout<<"print array fail!"<<endl;
return;
}
vector<string>::iterator it;
vector<vector<string>>::iterator iter;
vector<string> vec_tmp;
for(iter=strArray.begin()+row; iter != strArray.end(); iter++)
{
vec_tmp = *iter;
for(it = vec_tmp.begin()+col; it != vec_tmp.end(); it++)
cout << *it <<"\t";
cout << endl;
}
cout<<endl<<"print array success !"<<endl;
}
//another way to show strArray
void show_str_array_2(int row,int col){
//from row and col to show strArray
for (int i = row; i <strArray.size() ; ++i) {
for (int j = col; j <strArray[0].size() ; ++j) {
cout<<strArray[i][j] <<"\t" ; }
cout<<endl;
}
}
};
#endif //UNTITLED1_FILEREAD_H
2.代码2如下
//main.app
#include "FileRead.h"
int main() {
FileRead fr("C:/student-mat.csv");
fr.show_str_array(1,2); //from 1 row and 2 col to show
return 0;
}
FlowLiver Notes

浙公网安备 33010602011771号