C语言中读写二进制文件,文件的数据类型为float,废话少说,上代码:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
cv::Mat descr;
descr=cv::Mat::zeros(8,8,CV_32F);
FILE *fp=fopen("file_text.txt","w");
FILE *fp_bin=fopen("file_binary.bin","wb");
float cout=0;
for (int i=0; i<descr.rows; i++)
{
for (int j=0; j<descr.cols; j++)
{
cout+=1.0;
fprintf(fp,"%f ",cout);
descr.at<float>(i,j)=cout;
fwrite(&descr.at<float>(i,j) , sizeof(float) , 1 , fp_bin);
}
fprintf(fp,"\n");
}
fclose(fp);
fclose(fp_bin);
FILE *fin_bin=fopen("file_binary.bin","rb");
int rc;
float buf;
/*int k=1;*/
while ((rc=fread( &buf, sizeof(float) , 1 , fin_bin))!=0)
{
std::cout<<buf<<endl;
}
fclose(fin_bin);
return 0;
}