C++中實(shí)現(xiàn)保存數(shù)據(jù)到CSV文件
C++保存數(shù)據(jù)到CSV文件
主要是今天工作的時(shí)候需要把一些數(shù)據(jù)保存到本地,因?yàn)槭且恍╊A(yù)測(cè)值和標(biāo)簽的對(duì)比,還有預(yù)測(cè)值的概率,所以想到用CSV文件來保存,大概查了一下,還是比較簡(jiǎn)單的,所以記錄一下。
首先要說明的是CSV文件有點(diǎn)類似excel文件,也可以用excel文件直接打開的。在寫數(shù)據(jù)到CSV文件的時(shí)候要注意,字符串逗號(hào)表示的是換列,換行符號(hào)就是換行,所以在寫數(shù)據(jù)的時(shí)候要注意好這兩點(diǎn)就可以了。
然后寫的時(shí)候也很簡(jiǎn)單,直接用C++的ofstream就可以實(shí)現(xiàn)了,ofstream有一個(gè)操作是"<<",這個(gè)也很好用,就類似std::cout的操作一樣即可。
比如我現(xiàn)在的數(shù)據(jù)是比較統(tǒng)一的,每一個(gè)樣本是一行,一行數(shù)據(jù)要分成四列,第一列是樣本的圖像地址,第二列是標(biāo)簽,第三列是最終預(yù)測(cè)值,第四列是概率,每一列的格式是一樣的,那么我的代碼就是這樣:
ofstream file(CSV_PATH);
if (file)
{
? ? file << image_path << "," << label << "," << prediction << "," << probability << "\n";
}
file.close();這樣就可以實(shí)現(xiàn)了,還是比較容易的。
C++對(duì)csv文件操作(讀、寫、追加)
使用說明:csv文件按照","進(jìn)行分隔。因此每個(gè)內(nèi)容中需避免出現(xiàn)","
1.讀csv文件
c++通過文件讀入方式打開文件。即通過ifstream類進(jìn)行打開文件。
string fname = "test.csv";
//以讀入方式打開文件
ifstream csv_data(fname, ios::in);
if (!csv_data.is_open())
{
cout << "Error: opening file fail" << endl;
exit(1);
}
else {
string line;
vector<string> words; //聲明一個(gè)字符串向量
string word;
// ------------讀取數(shù)據(jù)-----------------
// 讀取標(biāo)題行
getline(csv_data, line);
istringstream sin;
// 按行讀取數(shù)據(jù)
while (getline(csv_data, line))
{
// 清空vector及字符串流,只存當(dāng)前行的數(shù)據(jù)
words.clear();
sin.clear();
sin.str(line);
//將字符串流sin中的字符讀到字符串?dāng)?shù)組words中,以逗號(hào)為分隔符
while (getline(sin, word, ','))
{
cout << word << endl;
words.push_back(word); //將每一格中的數(shù)據(jù)逐個(gè)push
}
}
csv_data.close();
}2.寫入csv文件
c++通過文件寫入方式打開文件進(jìn)行寫入。即通過ofstream類進(jìn)行寫入,并在打開文件中指明ios::out。
說明:默認(rèn)通過iso::out方式進(jìn)行寫入,當(dāng)文件不存在時(shí)會(huì)進(jìn)行創(chuàng)建
string fname = "test.csv"; ofstream outFile(fname, ios::out); // 寫入標(biāo)題行 outFile << "name" << ',' << "income" << ',' << "expenditure" << ',' << "addr" << endl; // ********寫入兩行數(shù)據(jù)********* outFile << "zhangsan" << ',' << "3000" << ',' << "1200" << ',' << "陜西省" << endl; outFile << "lisi" << ',' << to_string(2032.1) << ',' << to_string(789.2) << ',' << "北京市" << endl; //數(shù)字需轉(zhuǎn)為字符串進(jìn)行寫入,csv文件結(jié)束一行寫入需要"\n"或者endl進(jìn)行換行 outFile.close();
3.向csv文件中追加內(nèi)容
與第2部分幾乎相同,只不過是打開文件時(shí)選擇ios::app方式進(jìn)行。當(dāng)文件不存在時(shí)會(huì)進(jìn)行創(chuàng)建
ofstream outFile(fname, ios::app); // ********寫入兩行數(shù)據(jù)********* outFile << "wangwu" << ',' << "1234" << ',' << to_string(12.32) << ',' << "河南省" << endl; outFile << "lisi" << ',' << to_string(2032.1) << ',' << to_string(789.2) << ',' << "北京市" << endl; //數(shù)字需轉(zhuǎn)為字符串進(jìn)行寫入,csv文件結(jié)束一行寫入需要"\n"或者endl進(jìn)行換行 outFile.close();
4.具體使用
4.1讀入csv文件
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
using namespace std;
void PrintCSVLine(vector<string> line_data)
{
//此語(yǔ)法僅在C++11中適用
for (string str : line_data)
{
cout << str << " ";
}
cout << endl;
}
//讀入csv文件
int main() {
string fname = "test.csv";
//以讀入方式打開文件
ifstream csv_data(fname, ios::in);
if (!csv_data.is_open())
{
cout << "Error: opening file fail" << endl;
exit(1);
}
else {
string line;
vector<string> words; //聲明一個(gè)字符串向量
string word;
// ------------讀取數(shù)據(jù)-----------------
// 讀取標(biāo)題行
getline(csv_data, line);
istringstream sin;
// 按行讀取數(shù)據(jù)
while (getline(csv_data, line))
{
// 清空vector及字符串流,只存當(dāng)前行的數(shù)據(jù)
words.clear();
sin.clear();
sin.str(line);
//將字符串流sin中的字符讀到字符串?dāng)?shù)組words中,以逗號(hào)為分隔符
while (getline(sin, word, ','))
{
//cout << word << endl;
words.push_back(word); //將每一格中的數(shù)據(jù)逐個(gè)push
}
//輸出此行中的內(nèi)容
PrintCSVLine(words);
}
csv_data.close();
}
}4.2寫入csv文件
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string fname = "test.csv";
ofstream outFile(fname, ios::out);
if (outFile.is_open()) // 檢查文件是否打開成功
{
// 寫入標(biāo)題行
outFile << "name" << ','
<< "income" << ','
<< "expenditure" << ','
<< "addr" << endl;
// ********寫入兩行數(shù)據(jù)*********
outFile << "zhangsan" << ','
<< "3000" << ','
<< "1200" << ','
<< "陜西省" << endl;
outFile << "lisi" << ','
<< to_string(2032.1) << ','
<< to_string(789.2) << ','
<< "北京市" << endl;
//數(shù)字需轉(zhuǎn)為字符串進(jìn)行寫入,csv文件結(jié)束一行寫入需要"\n"或者endl進(jìn)行換行
outFile.close();
}
else
{
cout << "文件無法打開!" << endl;
}
}
4.3向csv文件追加
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string fname = "test.csv";
//先判斷文件是否存在
ifstream file(fname);
if (!file.is_open()) {
cout << "File does not exist!" << endl;
return 1;
}
else {
cout << "File exists!" << endl;
file.close();//必須先關(guān)閉文件后才可寫入
ofstream outFile(fname, ios::app);
// ********寫入兩行數(shù)據(jù)*********
outFile << "wangwu" << ','
<< "1234" << ','
<< to_string(12.32) << ','
<< "河南省" << endl;
outFile << "lisi" << ','
<< to_string(2032.1) << ','
<< to_string(789.2) << ','
<< "北京市" << endl;
//數(shù)字需轉(zhuǎn)為字符串進(jìn)行寫入,csv文件結(jié)束一行寫入需要"\n"或者endl進(jìn)行換行
outFile.close();
}
return 0;
}
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
C++實(shí)現(xiàn)自定義撤銷重做功能的示例代碼
在使用c++做界面開發(fā)的時(shí)候,尤其是實(shí)現(xiàn)白板功能時(shí)需要自己實(shí)現(xiàn)一套撤銷重做功能.如果是qt則有QUndoable對(duì)象,可以直接拿來用。但是如果是使用gdi繪圖,則可能需要自己實(shí)現(xiàn)了。本文就來用C++實(shí)現(xiàn)自定義撤銷重做功能,需要的可以參考一下2022-12-12
C/C++利用棧和隊(duì)列實(shí)現(xiàn)停車場(chǎng)管理系統(tǒng)
數(shù)據(jù)結(jié)構(gòu)的課程設(shè)計(jì)一般都不是很好理解,今天小編為大家總結(jié)了一下c和c++版本的常見棧和隊(duì)列的的停車場(chǎng)管理程序,需要的小伙伴可以參考一下2022-06-06
C語(yǔ)言實(shí)現(xiàn)返回字符串函數(shù)的四種方法
在C語(yǔ)言中實(shí)現(xiàn)函數(shù)返回字符串,首先要確定函數(shù)返回的字符串地址的來源,一般分為四種方式,下面這篇文章就給大家通過示例代碼詳細(xì)介紹這幾種方法,有需要的朋友們可以參考借鑒,下面來一起看看吧。2016-12-12
c/c++ 標(biāo)準(zhǔn)庫(kù) bind 函數(shù)詳解
bind是一組用于函數(shù)綁定的模板。在對(duì)某個(gè)函數(shù)進(jìn)行綁定時(shí),可以指定部分參數(shù)或全部參數(shù),也可以不指定任何參數(shù),還可以調(diào)整各個(gè)參數(shù)間的順序。這篇文章主要介紹了c/c++ 標(biāo)準(zhǔn)庫(kù) bind 函數(shù) ,需要的朋友可以參考下2018-09-09
Qt項(xiàng)目實(shí)戰(zhàn)之實(shí)現(xiàn)MP3音樂播放器
這篇文章主要為大家詳細(xì)介紹了如何利用Qt實(shí)現(xiàn)MP3音樂播放器,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴可以了解一下2023-03-03

