生活随笔
收集整理的這篇文章主要介紹了
C++读取读取csv、xls文件的类
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
由于經(jīng)常要讀取大量的數(shù)據(jù)進(jìn)行分析,自己封裝了一個(gè)C++的類,代替網(wǎng)上的rapidcsv庫。一切以自己的需求為準(zhǔn),夠用簡略就好。真正體會到了C++程序員造輪子的樂趣。自己的拼裝車就是好用。
文章需要的csv、xls數(shù)據(jù)文件,大家可以自己從網(wǎng)上下載:
1、網(wǎng)易財(cái)經(jīng)頻道可以下載到csv格式的股票日線數(shù)據(jù),可以試著用我的quickcsv庫讀取
2、同花順股票軟件也可以導(dǎo)出xls格式的excel文件,也可以用我的庫來讀取。
所以,本文就不附加下載內(nèi)容了,自己找材料測試
今天廢話真多,好了,上代碼:
QuickCsv.h頭文件
#pragma once
#ifndef QUICKCSV_H
#define QUICKCSV_H
#include<vector>
#include<iostream>
#include <fstream>
#include <sstream>
#include<assert.h>using namespace std
;class QuickCsv
{
public:QuickCsv(){}~QuickCsv(){VectorCsvOfAllLines
.clear();}void InitData(std
::string Path
){mPath
= Path
;VectorCsvOfAllLines
.clear();ReadCsvData(Path
);}void WriteInDataToCsv(std
::string Path
, std
::string VectorDataLine
, const char* Mode
){FILE
* mFile
;fopen_s(&mFile
, Path
.c_str(), Mode
);if (!mFile
){printf_s("文件不存在,打開失敗!!!");return;}fprintf_s(mFile
, VectorDataLine
.c_str());fflush(mFile
);fclose(mFile
);}void WriteInDataToCsv(std
::string Path
, std
::vector
<std
::string
>VectorDataLine
, const char* Mode
){FILE
* mFile
;fopen_s(&mFile
, Path
.c_str(), Mode
);if (!mFile
){printf_s("文件不存在,打開失敗!!!");return;}for (auto itr
= VectorDataLine
.begin(); itr
!= VectorDataLine
.end(); ++itr
){if (itr
== VectorDataLine
.end() - 1){fprintf_s(mFile
, "%s\0", itr
->c_str());fflush(mFile
);}else{fprintf_s(mFile
, "%s\t", itr
->c_str());fflush(mFile
);}}fclose(mFile
);}void WriteInDataToCsv(std
::string Path
, std
::vector
<std
::vector
<std
::string
>>VectorDataLine
, const char* Mode
){FILE
* mFile
;fopen_s(&mFile
, Path
.c_str(), Mode
);if (!mFile
){printf_s("文件不存在,打開失敗!!!");return;}for (auto itr
= VectorDataLine
.begin(); itr
!= VectorDataLine
.end(); ++itr
){for (auto data_itr
= itr
->begin(); data_itr
!= itr
->end(); ++data_itr
){if (data_itr
== itr
->end() - 1){fprintf_s(mFile
, "%s\0", data_itr
->c_str());fflush(mFile
);}else{fprintf_s(mFile
, "%s\t", data_itr
->c_str());fflush(mFile
);}}}fclose(mFile
);}void DeletRow(int RowID
){for (int i
= 0; i
< VectorCsvOfAllLines
.size(); i
++){if (i
== RowID
){continue;}TmpVectorCsvOfAllLines
.push_back(VectorCsvOfAllLines
.at(i
));}VectorCsvOfAllLines
.swap(TmpVectorCsvOfAllLines
); TmpVectorCsvOfAllLines
.clear(); WriteInDataToCsv(mPath
, VectorCsvOfAllLines
, "w");}void DeleteColumn(int ColumnID
){std
::vector
<std
::string
>tmpLine
;for (auto itr
= VectorCsvOfAllLines
.begin(); itr
!= VectorCsvOfAllLines
.end(); ++itr
){int num
= 0;for (auto lineitr
= itr
->begin(); lineitr
!= itr
->end();++lineitr
){if (num
== ColumnID
){continue;}else{tmpLine
.push_back(*lineitr
);}num
++;}TmpVectorCsvOfAllLines
.push_back(tmpLine
);tmpLine
.clear();}VectorCsvOfAllLines
.swap(TmpVectorCsvOfAllLines
); TmpVectorCsvOfAllLines
.clear(); WriteInDataToCsv(mPath
, VectorCsvOfAllLines
, "w");}void InsertRow(int RowID
,std
::vector
<std
::string
>myLine
){std
::vector
<std
::vector
<std
::string
>>::iterator myitr
;int num
= 0;for (auto itr
= VectorCsvOfAllLines
.begin(); itr
!= VectorCsvOfAllLines
.end(); ++itr
){if (num
== RowID
){myitr
= itr
--;break;}else{num
++;}}VectorCsvOfAllLines
.insert(myitr
, myLine
);WriteInDataToCsv(mPath
, VectorCsvOfAllLines
, "w");}void InsertColumn(int ColumnID
,std
::vector
<std
::string
>myColumData
){std
::vector
<std
::string
>tmpLine
;int RowID
= 0;for (auto itr
= VectorCsvOfAllLines
.begin(); itr
!= VectorCsvOfAllLines
.end(); ++itr
){RowID
++;tmpLine
= *itr
;std
::vector
<std
::string
>::iterator myitr
= GetIteratorByID(ColumnID
, tmpLine
);if (RowID
< myColumData
.size()){tmpLine
.insert(myitr
, myColumData
.at(RowID
));TmpVectorCsvOfAllLines
.push_back(tmpLine
);}tmpLine
.clear();}VectorCsvOfAllLines
.swap(TmpVectorCsvOfAllLines
); TmpVectorCsvOfAllLines
.clear(); WriteInDataToCsv(mPath
, VectorCsvOfAllLines
, "w");}std
::vector
<std
::string
> GetColumStrings(int ColumID
, bool GetHeader
){std
::vector
<std
::string
> VectorCsvRowNames
;std
::string tmpStr
;for (auto itr
= VectorCsvOfAllLines
.begin(); itr
!= VectorCsvOfAllLines
.end(); ++itr
){tmpStr
= itr
->at(ColumID
);VectorCsvRowNames
.push_back(tmpStr
);tmpStr
.clear();}if (!GetHeader
){VectorCsvRowNames
.erase(VectorCsvRowNames
.begin());}return VectorCsvRowNames
;}std
::vector
<std
::vector
<std
::string
>> GetColumsStrings(std
::vector
<int> ColumIDs
, bool GetHeader
){std
::vector
<std
::vector
<std
::string
>> VectorCsvRowNames
;if (VectorCsvOfAllLines
.size() == 0){std
::cout
<< "類內(nèi)部GetColumsStrings函數(shù)調(diào)用時(shí)VectorCsvOfAllLines沒有數(shù)據(jù):" << std
::endl
;return VectorCsvRowNames
;}std
::vector
<std
::string
>oneLine
;for (auto itr
= VectorCsvOfAllLines
.begin(); itr
!= VectorCsvOfAllLines
.end(); ++itr
){for (size_t i
= 0; i
< ColumIDs
.size(); i
++){oneLine
.push_back(itr
->at(ColumIDs
[i
]));}VectorCsvRowNames
.push_back(oneLine
);oneLine
.clear();}if (!GetHeader
){VectorCsvRowNames
.erase(VectorCsvRowNames
.begin());}return VectorCsvRowNames
;}std
::vector
<std
::string
> GetHeaders(){return VectorCsvOfAllLines
.at(0);}std
::vector
<double>GetRowData(int RowID
, bool IsRowName
, bool IsHeader
){if (IsHeader
){RowID
+= 1;}std
::vector
<double> VectorCsvRowDatas
;std
::vector
<std
::string
> VectormyLine
= VectorCsvOfAllLines
.at(RowID
);if (IsRowName
){for (auto itr
= VectormyLine
.begin() + 1; itr
!= VectormyLine
.end(); ++itr
){VectorCsvRowDatas
.push_back(StringToDouble(*itr
));}}else{for (auto itr
= VectormyLine
.begin(); itr
!= VectormyLine
.end(); ++itr
){VectorCsvRowDatas
.push_back(StringToDouble(*itr
));}}return VectorCsvRowDatas
;}std
::vector
<double>GetRowData(int RowID
, std
::vector
<int>ColumsID
, bool IsHeader
){if (IsHeader
){RowID
+= 1;}std
::vector
<double> VectorCsvRowDatas
;std
::vector
<std
::string
> VectormyLine
= VectorCsvOfAllLines
.at(RowID
);for (auto int_itr
= ColumsID
.begin(); int_itr
!= ColumsID
.end(); ++int_itr
){VectorCsvRowDatas
.push_back(StringToDouble(VectormyLine
[*int_itr
]));}return VectorCsvRowDatas
;}
private:std
::vector
<std
::string
>::iterator
GetIteratorByID(int DataID
, std
::vector
<std
::string
>MyVector
){std
::vector
<std
::string
>::iterator myitr
;int num
= 0;for (auto lineitr
= MyVector
.begin(); lineitr
!= MyVector
.end(); ++lineitr
){if (num
== DataID
){myitr
= lineitr
;break;}else{num
++;}}return myitr
;}void ReadCsvData(std
::string Path
){FILE
* mFile
;fopen_s(&mFile
, Path
.c_str(), "r");if (!mFile
){printf_s("文件%s不存在、或者文件名、文件路徑有誤,文件打開失敗!!!\n", Path
.c_str());return;}fclose(mFile
);std
::ifstream
fin(Path
.c_str()); std
::string line
;getline(fin
, line
);if (line
.find(',') != std
::string
::npos
){VectorCsvOfAllLines
.push_back(ParseLineStringToVector(line
, ','));while (getline(fin
, line
)){std
::vector
<std
::string
> VectorCsvDataOfOneLine
= ParseLineStringToVector(line
, ',');VectorCsvOfAllLines
.push_back(VectorCsvDataOfOneLine
);VectorCsvDataOfOneLine
.clear();}}else if (line
.find('\t') != std
::string
::npos
){VectorCsvOfAllLines
.push_back(ParseLineStringToVector(line
, '\t'));while (getline(fin
, line
)){std
::vector
<std
::string
> VectorCsvDataOfOneLine
= ParseLineStringToVector(line
, '\t');VectorCsvOfAllLines
.push_back(VectorCsvDataOfOneLine
);VectorCsvDataOfOneLine
.clear();}}else{while (getline(fin
, line
)){std
::vector
<std
::string
> VectorCsvDataOfOneLine
;VectorCsvDataOfOneLine
.push_back(line
);VectorCsvOfAllLines
.push_back(VectorCsvDataOfOneLine
);VectorCsvDataOfOneLine
.clear();}}}std
::vector
<std
::string
> ParseLineStringToVector(std
::string LineStr
, char ch
){std
::istringstream
sin(LineStr
); std
::string StrOfOneLine
;std
::vector
<std
::string
> VectorLineStr
;while (getline(sin
, StrOfOneLine
, ch
)){VectorLineStr
.push_back(Trim(StrOfOneLine
)); LineStr
.clear();}return VectorLineStr
;}std
::string
Trim(std
::string
& str
){str
.find_first_not_of(" \t\r\n");str
.erase(0, str
.find_first_not_of(" \t\r\n"));str
.erase(str
.find_last_not_of(" \t\r\n") + 1);return str
;}double StringToDouble(std
::string str
){assert(str
.size() != NULL);int i
= 0;double dou_num
= 0;double t
= 10;bool fh_
= false;if (str
[i
] == '-'){fh_
= true;i
++;}while (str
[i
] != '\0'){if (str
[i
] == '.'){i
++;break;}dou_num
= dou_num
* 10 + str
[i
] - '0';i
++;}while (str
[i
] != '\0'){dou_num
= dou_num
+ ((double)str
[i
] - '0') / t
;t
*= 10;i
++;}if (fh_
)return -1.0 * dou_num
;elsereturn dou_num
;}
private:std
::string mPath
;std
::vector
<std
::vector
<std
::string
>> VectorCsvOfAllLines
;std
::vector
<std
::vector
<std
::string
>> TmpVectorCsvOfAllLines
;int ColumnNum
= 0;
};#endif
調(diào)用QuickCsv類的cpp文件
#include"QuickCsv.h"
QuickCsv
qc("../shanghai_a.xls");
int main()
{int RowNum
=6;vector
<int>myColums
= { 0,1,4,7 };vector
<vector
<string
>> myVector
=qc
.GetColumsStrings(myColums
,1);int i
= 0;for (auto itr
= myVector
.begin(); itr
!= myVector
.end(); ++itr
){if (i
> 50)break;for (auto mitr
= itr
->begin(); mitr
!= itr
->end(); ++mitr
){if (mitr
!= itr
->end() - 1){printf_s("%s---", mitr
->c_str());}else{printf_s("%s\n", mitr
->c_str());}}i
++;}myColums
.clear();myVector
.clear();getchar();qc
.~QuickCsv();return 0;
}
效果圖:
總結(jié)
以上是生活随笔為你收集整理的C++读取读取csv、xls文件的类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。