日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

bin文件读写 - C/C++

發(fā)布時間:2023/12/16 c/c++ 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 bin文件读写 - C/C++ 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文介紹一下 C 和 C++ 讀取和保存 bin 文件的方法。

??bin 文件的存取在調(diào)試網(wǎng)絡(luò)推理定位問題的時候可能會經(jīng)常用到,如在這個框架里網(wǎng)絡(luò)輸出和預(yù)期對不上,經(jīng)常需要把這個網(wǎng)絡(luò)里的前處理輸出、網(wǎng)絡(luò)推理輸出搬到另外的框架里走一遍,來確定是前處理有問題,還是網(wǎng)絡(luò)推理有問題,還是后處理有問題。這里分享一下 C 語言和 C++ 讀取和保存特征數(shù)據(jù)為 bin 文件的方法。其實大部分情況可以用 C++ 搞定,但如 darknet 這種純 C 框架可能就需要用 C 實現(xiàn)。文章目錄
1、C 讀取和保存 bin 文件
1.1 C 讀取
1.2 C 保存
1.3 C 調(diào)用
2、C++ 讀取和保存 bin 文件
2.1 C++ 讀取
2.2 C++ 保存
2.3 C++ 調(diào)用
1、C 讀取和保存 bin 文件
1.1 C 讀取
/// C 讀取bin文件

int getBinSize(char *path) {int ?size = 0;FILE ?*fp = fopen(path, "rb");if (fp){fseek(fp, 0, SEEK_END);size = ftell(fp);fclose(fp);}printf("\npath=%s,size=%d \n", path, size);return size; }void readBin(char *path, char *buf, int size) {FILE *infile;if ((infile = fopen(path, "rb")) == NULL){printf("\nCan not open the path: %s \n", path);exit(-1);}fread(buf, sizeof(char), size, infile);fclose(infile); }


1.2 C 保存
/// C 保存bin文件

void writeBin(char *path, char *buf, int size) {FILE *outfile;if ((outfile = fopen(path, "wb")) == NULL){printf("\nCan not open the path: %s \n", path);exit(-1);}fwrite(buf, sizeof(char), size, outfile);fclose(outfile); }


1.3 C 調(diào)用

// read binFile char filePath[] = "./demo.bin"; int size = GetBinSize(filePath); char *buf = (char*)malloc(size); readBin(filePath, buf, size); float *fbuf = (float*)buf;// write binFile char saveFilePath[] = "./demo_saved.bin" writeBin(saveFilePath, buf, size)free(buf)

2、C++ 讀取和保存 bin 文件
2.1 C++ 讀取

/// C++ 讀取bin文件 void getBinSize(std::string path) {int size = 0;std::ifstream infile(path, std::ifstream::binary);infile.seekg(0, infile.end);int size= infile.tellg();infile.seekg(0, infile.beg);infile.close();printf("\npath=%s,size=%d \n", path, size);return size; }void readBin(std::string path, char *buf, int size) {std::ifstream infile(path, std::ifstream::binary);infile.read(static_cast<char *>(buf), size);infile.close(); }


2.2 C++ 保存

/// C++ 保存bin文件 void writeBin(std::string path, char *buf, int size) {std::ofstream outfile(path, std::ifstream::binary);outfile.write((char *)(buf), size);outfile.close(); }


2.3 C++ 調(diào)用

// read binFile std::string filePath= "./demo.bin"; int size = GetBinSize(filePath); char *buf= new char[size]; readBin(filePath, buf, size); float *fbuf = reinterpret_cast<float *>(buf);// write binFile std::string saveFilePath= "./demo_saved.bin"; writeBin(saveFilePath, buf, size);delete buf;

2. 文件讀出成char

//C方式, 調(diào)用的函數(shù)繁多 //fopen,fseek,ftell,fseek,malloc,fread,fclose,free. void foo() {FILE* fp=fopen(sFileName,"rb");fseek(fp,0,SEEK_END);int len = ftell(fp);fseek(fp,0,SEEK_SET);char* s = (char*)malloc(len);fread(s,1,len,fp);fclose(fp);fwrite(s,1,len,stdout);//outputfree(s); } //C++方式,易懂 void foo() {ifstream fs(sFileName.c_str(),ios::binary);stringstream ss ; ss << fs.rdbuf();fs.close();string str = ss.str();//read into string } //C++方式,高大上 //string的構(gòu)造用了一個模版函數(shù) void foo() {std::ifstream ifs(sFileName.c_str());std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>(0));ifs.close(); }

總結(jié)

以上是生活随笔為你收集整理的bin文件读写 - C/C++的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。