1/10/2013

C file Write, Read by Binary - example source code

C file Write, Read by Binary - example source code


#include < stdio.h >
#include < stdlib.h >
#include < time.h >


void main()
{


 //////////////////////////////////////////////////////////////////////////
 //File Write by binary
 srand( time(0));
 float feature[10];
 for(int i=0; i<10; ++i) 
  feature[i] = rand()/100; 

 //data confirm
 for(int i=0; i< 10; ++i) 
  printf("%lf\n", feature[i] );

 FILE * fp;
 fp = fopen("output.txt", "wb");
 fwrite(feature, sizeof(float), 10, fp);
 fclose(fp);

 printf("//////////////////////////////////////////////////////////////////////////\n");


 //////////////////////////////////////////////////////////////////////////
 //File Read by binary
 float feature_r[10];
 FILE* fp2;
 fp2 = fopen("output.txt", "rb");
 fread(feature_r, sizeof(float), 10, fp2);
 fclose(fp2);

 //data confirm
 for(int i=0; i< 10; ++i) 
  printf("%lf\n", feature_r[i] );
 //////////////////////////////////////////////////////////////////////////


}




No comments:

Post a Comment