天天看點

學生資訊導出為csv檔案c語言,C++ 把數組資料存入 CSV 檔案,以及讀取 CSV 檔案的資料...

2. 代碼

#pragma once

//Microsoft Visual Studio 2015 Enterprise

#include

#include

#include

#include

#include

#include

using namespace std;

template

class modifyCSVfile

{

private:

struct arrInfo

{

double **arrName;

int lineNum;

int rowNum;

};

public:

string saveArray(base_T* arr, int lineNum, int rowNum, string fileName, int precis = 6);

string saveArray(const vector>& arr, string fileName, int precis = 6);

vector> CSVtoVector(string fileName);

arrInfo CSVtoArray(string fileName);

int delArray(arrInfo);

};

//*************************************************************************************************************************************************************

//*************************************************************************************************************************************************************

template

string modifyCSVfile::saveArray(base_T* arr, int lineNum, int rowNum, string fileName, int precis)

//函數功能:把一個int/float/double型二維數組,存入CSV檔案

//參數1:數組第一個元素位址,e.g.【&array1[0][0]】;參數2:數組行數;參數3:數組列數;參數4:檔案名;參數5:設定精度(預設精度是6)

{

fileName += ".csv"; //儲存成VSV格式檔案,友善用Excel打開

//儲存數組到檔案。如果檔案不存在,建立檔案,并寫入資料;如果檔案存在,清空重新寫入

ofstream fout;

fout.open(fileName.c_str(), ios_base::trunc);

fout << showpoint;

fout.precision(precis);

for (int i = 0; i < lineNum; i++)

{

for (int j = 0; j < rowNum; j++)

{

if (j < rowNum - 1)

fout << *(arr + i * rowNum + j) << ","; // arr + i * rowNum + j:找到目前數組元素的順序索引值

else

fout << *(arr + i * rowNum + j) << endl;

}

}

fout.close();

return fileName;

}

用法示例:Visual Studio 2017 Community

//#include

//#include

//#include

//#include

//#include"modifyCSVfile.h"

//

//const int arr_lineNum = 5;

//const int arr_rowNum = 7;

//int main()

//{

// //定義一個double型二維數組,并指派

// double k = 1.1;

// double arr[arr_lineNum][arr_rowNum];

// for (int i = 0; i < arr_lineNum; i++)

// {

// for (int j = 0; j < arr_rowNum; j++)

// {

// arr[i][j] = k;

// k = k + 1;

// }

// }

//

// //輸出目前數組到螢幕

// for (int i = 0; i < arr_lineNum; i++)

// {

// for (int j = 0; j < arr_rowNum; j++)

// {

// cout << arr[i][j] << " ";

// }

// cout << endl;

// }

// system("pause");

//

// //把目前數組存如檔案。檔案位置:目前工程檔案夾下。檔案格式為.csv,可用文本文檔打開,也可用Excel打開。

// modifyCSVfile save;

// save.saveArray(&arr[0][0], arr_lineNum, arr_rowNum, "arr1", 6);

//

// return 0;

//}

//*************************************************************************************************************************************************************

//*************************************************************************************************************************************************************

template

string modifyCSVfile::saveArray(const vector>& arr, string fileName, int precis)

//函數功能:把一個vector二維數組,存入CSV檔案

//參數1:vector對象名;參數2:檔案名;參數3:設定精度(預設精度是6)

{

fileName += ".csv"; //儲存成VSV格式檔案,友善用Excel打開

//儲存數組到檔案。如果檔案不存在,建立檔案,并寫入資料;如果檔案存在,清空重新寫入

ofstream fout;

fout.open(fileName.c_str(), ios_base::trunc);

fout << showpoint;

fout.precision(precis);

for (unsigned int i = 0; i < arr.size(); i++)

{

for (unsigned int j = 0; j < arr[i].size(); j++)

{

if (j < arr[i].size() - 1)

fout << arr[i][j] << ",";

else

fout << arr[i][j] << endl;

}

}

fout.close();

return fileName;

}

用法示例:Visual Studio 2017 Community

//#include

//#include

//#include

//#include

//#include"modifyCSVfile.h"

//

//int main()

//{

// //定義一個二維vector數組,并指派

// double k = 1.1;

// int lineNum = 5, rowNum = 7;

// vector > arr2(lineNum, vector(rowNum));

// for (int i = 0; i < lineNum; i++)

// {

// for (int j = 0; j < rowNum; j++)

// {

// arr2[i][j] = k;

// k = k + 1;

// }

// }

//

// //輸出目前數組到螢幕

// for (int i = 0; i < lineNum; i++)

// {

// for (int j = 0; j < rowNum; j++)

// cout << arr2[i][j] << " ";

// cout << endl;

// }

// system("pause");

//

// //把目前數組存如檔案。檔案位置:目前工程檔案夾下。檔案格式為.csv,可用文本文檔打開,也可用Excel打開。

// modifyCSVfile save;

// save.saveArray(arr2, "arr2", 6);

//

// return 0;

//}

//*************************************************************************************************************************************************************

//*************************************************************************************************************************************************************

template

vector> modifyCSVfile::CSVtoVector(string fileName)

//函數功能:讀取隻包含資料不包含文字的CSV檔案,并取出裡邊的資料存入到二維double型vector數組中

//參數1:檔案名

{

fileName += ".csv";

ifstream fin;

fin.open(fileName.c_str(), ios_base::in); //以隻讀的方式打開檔案

//跳過CSV檔案開頭可能出現的空行

char ch;

while (fin.get(ch))

{

if (ch == '\n')

continue;

else

break;

}

streamoff pos = fin.tellg(); //儲存目前位置

fin.seekg(pos - 1, ios_base::beg); //傳回到檔案中有資料開始的那一行的首位

//擷取CSV檔案中資料的行數

int lineNum = 0, rowNum = 0;

string buf;

while (getline(fin, buf) && !buf.empty())

{

lineNum = lineNum + 1;

}

fin.clear(); //getline()讀取到檔案尾,接下來輸入流被阻斷。需要重置輸入流,如果不重置,接下來将無法擷取檔案資料。

//擷取CSV檔案中資料的列數

fin.seekg(pos - 1, ios_base::beg); //傳回到檔案中有資料開始的那一行的首位

while (fin.get(ch))

{

if (ch == ',')

{

rowNum = rowNum + 1;

}

else

if (ch == '\n')

break;

}

rowNum = rowNum + 1;

//把CSV檔案中的資料存入double型的vector中

fin.seekg(pos - 1, ios_base::beg); //傳回到檔案中有資料開始的那一行的首位

buf.erase(0);

double temp;

vector>vect(lineNum, vector(rowNum));

for (int i = 0; i < lineNum; i++)

{

for (int j = 0; j < rowNum; j++)

{

while (fin.get(ch))

{

if (ch != ',' && ch != '\n')

buf += ch;

else

{

temp = atof(buf.c_str());

vect[i][j] = temp;

buf.erase(0);

break;

}

}

}

}

fin.close();

return vect;

}

用法示例:Visual Studio 2017 Community

//#include

//#include

//#include

//#include

//#include

//#include

//#include"modifyCSVfile.h"

//

//using namespace std;

//

//int main()

//{

//

// vector> arr1; //建立一個二維vector數組,用于接受函數調用傳回的二維vector數組

// modifyCSVfile read;

// arr1 = read.CSVtoVector("arr2");

//

// //輸出arr1到螢幕

// for (int i = 0; i < 4; i++)

// {

// for (int j = 0; j < 4; j++)

// {

// cout << arr1[i][j] << " ";

// }

// cout << endl;

// }

//

// system("pause");

// return 0;

//}

//*************************************************************************************************************************************************************

//*************************************************************************************************************************************************************

template

typename modifyCSVfile::arrInfo modifyCSVfile::CSVtoArray(string fileName)

//函數功能:讀取隻包含資料不包含文字的CSV檔案,并取出裡邊的資料存入到new建立的動态二維數組中

//參數1:檔案名

{

fileName += ".csv";

ifstream fin;

fin.open(fileName.c_str(), ios_base::in); //以隻讀的方式打開檔案

//跳過CSV檔案開頭可能出現的空行

char ch;

while (fin.get(ch))

{

if (ch == '\n')

continue;

else

break;

}

streamoff pos = fin.tellg(); //儲存目前位置

fin.seekg(pos - 1, ios_base::beg); //傳回到檔案中有資料開始的那一行的首位

//擷取CSV檔案中資料的行數

int lineNum = 0, rowNum = 0;

string buf;

while (getline(fin, buf) && !buf.empty())

{

lineNum = lineNum + 1;

}

fin.clear(); //getline()讀取到檔案尾,接下來輸入流被阻斷。需要重置輸入流,如果不重置,接下來将無法擷取檔案資料。

//擷取CSV檔案中資料的列數

fin.seekg(pos - 1, ios_base::beg); //傳回到檔案中有資料開始的那一行的首位

while (fin.get(ch))

{

if (ch == ',')

{

rowNum = rowNum + 1;

}

else

if (ch == '\n')

break;

}

rowNum = rowNum + 1;

//new建立二維動态數組

double **arr = new double *[lineNum];

for (int i = 0; i < lineNum; i++)

{

arr[i] = new double[rowNum];

}

//把CSV檔案中的資料存入二維數組中

fin.seekg(pos - 1, ios_base::beg); //傳回到檔案中有資料開始的那一行的首位

buf.erase(0);

double temp;

for (int i = 0; i < lineNum; i++)

{

for (int j = 0; j < rowNum; j++)

{

while (fin.get(ch))

{

if (ch != ',' && ch != '\n')

buf += ch;

else

{

temp = atof(buf.c_str());

arr[i][j] = temp;

buf.erase(0);

break;

}

}

}

}

fin.close();

//建立一個結構對象,儲存:指向目前數組的指針、目前數組行數、目前數組列數

arrInfo info;

info.arrName = arr;

info.lineNum = lineNum;

info.rowNum = rowNum;

return info; //傳回結構

}

用法示例:Visual Studio 2017 Community

//#include

//#include

//#include

//#include

//#include

//#include

//#include"modifyCSVfile.h"

//

//using namespace std;

//

//int main()

//{

// //讀取CSV檔案arr2中的資料,傳回一個結構。結構包括:指向數組的指針、數組行數、數組列數

// modifyCSVfile read;

// auto arr1 = read.CSVtoArray("arr2");

//

// //輸出數組資料到螢幕

// for (int i = 0; i < arr1.lineNum; i++)

// {

// for (int j = 0; j < arr1.rowNum; j++)

// {

// cout << arr1.arrName[i][j] << " ";

// }

// cout << endl;

// }

//

// //注意:由于調用CSVtoArray函數時,生成的數組是用new配置設定的,是以資料用完之後要記得釋放。

// //這裡沒有釋放,可以調用delArray函數釋放。

//

// system("pause");

// return 0;

//}

//*************************************************************************************************************************************************************

//*************************************************************************************************************************************************************

template

int modifyCSVfile::delArray(arrInfo info)

//函數功能:删除調用CSVtoArray函數時生成的動态二維數組

//由于CSVtoArray函數裡邊的二維數組是使用new建立的,是以用完之後要釋放

{

for (int i = 0; i < info.lineNum; i++)

delete[] info.arrName[i];

delete[] info.arrName;

return 0;

}

用法示例:Visual Studio 2017 Community

//#include

//#include

//#include

//#include

//#include

//#include

//#include"modifyCSVfile.h"

//

//using namespace std;

//

//int main()

//{

// //讀取CSV檔案arr2中的資料,傳回一個結構。結構包括:指向數組的指針、數組行數、數組列數

// modifyCSVfile read;

// auto arr1 = read.CSVtoArray("arr2");

//

// //輸出數組資料到螢幕

// for (int i = 0; i < arr1.lineNum; i++)

// {

// for (int j = 0; j < arr1.rowNum; j++)

// {

// cout << arr1.arrName[i][j] << " ";

// }

// cout << endl;

// }

//

// //釋放調用CSVtoArray函數時用new生成的動态二維數組

// modifyCSVfile del;

// del.delArray(arr1);

//

// system("pause");

// return 0;

//}

未完 ......

點選通路原文(進入後根據右側标簽,快速定位到本文)