C语言读写二进制文件(float)

C语言在opencv中读写二进制文件,文件的数据类型为float,废话少说,上代码:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include
#include
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  {
  for (int j=0; j   {
   cout+=1.0;
   fprintf(fp,"%f ",cout);
   descr.at(i,j)=cout;

   fwrite(&descr.at(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)
 {
  printf("%f\n",cout);
 }


 fclose(fin_bin);

 return 0;

}

你可能感兴趣的:(读写二进制文件)