日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網(wǎng)站首頁 編程語言 正文

C++中如何將數(shù)據(jù)保存為CSV文件_C 語言

作者:Mz楓 ? 更新時間: 2022-12-10 編程語言

C++將數(shù)據(jù)保存為CSV文件

因為最近涉及到保存模型推理結(jié)果的輸出文件,所以學一學如何將數(shù)據(jù)保存為CSV文件,比如保存檢測框box的頂點,置信度,label,入侵檢測的結(jié)果等。

用到的也是C++的ofstream,ofstream有一個操作是"<<",這個也很好用,就類似std::cout的操作一樣即可。

比如我現(xiàn)在的數(shù)據(jù)是比較統(tǒng)一的,每一個樣本是一行,一行數(shù)據(jù)要分成四列,第一列是樣本的圖像地址,第二列是標簽,第三列是最終預測值,第四列是概率,每一列的格式是一樣的

那么我的代碼就是這樣:

ofstream file(CSV_PATH);
if (file)
{
? ? file << image_path << "," << label << "," << prediction << "," << probability << "\n";
}
file.close();

要注意的是,逗號表示的是換列,換行符號就是換行。

CSV文件可以用excel直接打開

如何存儲CSV文件

應用工程里,經(jīng)常會遇到存儲一些數(shù)據(jù),存儲下來進行分析

#include <iostream>
#include <fstream>

using namespace std;

std::string CSV_PATH = "./data.csv";
ofstream csv;
int frame_num = 0;

struct Name
{
    int age;
    float height;
    float weight;
    int score;
};

int main()
{
    Name Zhangsan{30, 1.75, 78, 98};
    csv.open(CSV_PATH);

    while (true)
    {
        if (csv.is_open())
        {
            frame_num++;
            if (frame_num == 1)
            {
                csv << "age"
                    << ","
                    << "height(m)"
                    << ","
                    << "weight(kg)"
                    << ","
                    << "score"
                    << "\n";
            }
            else
            {
                csv << Zhangsan.age << "," << Zhangsan.height << "," << Zhangsan.weight << "," << Zhangsan.score
                    << "\n";
            }
        }
        if (frame_num > 10000) {
            csv.close();
        }
    }

    return 0;
}

原文鏈接:https://blog.csdn.net/u012456019/article/details/101481424

欄目分類
最近更新