首页 > 其他 > 详细

【转】二进制文件的读写浮点数

时间:2015-09-24 16:12:51      阅读:238      评论:0      收藏:0      [点我收藏+]

原文网址:http://www.myexception.cn/c/243743.html

 

------解决方案--------------------
#include <sys\stat.h> 
#include <stdio.h> 
#include <stdlib.h> 

int main(int argc, char *argv[]) 

int i; 
float farr[5]={1.0, 2.0, 3.0, 4.0, 5.0}, *farray; 
FILE *fp = fopen( "data.txt ", "wb "); 

for(i=0; i <5; i++) 
fwrite(&farr[i], sizeof(float), 1, fp); 
fclose(fp); 

struct stat buf; 
if(stat( "data.txt ", &buf)) // get the file size 
exit(-1); 
int num = buf.st_size/sizeof(float); 
farray = (float *)malloc(num * sizeof(float)); //buf.st_size); 
fp=fopen( "data.txt ", "rb "); 
fread(farray, sizeof(float), num, fp); 
for(i=0; i <5; i++) 
printf( "%f, %f\n ", farr[i], farray[i]); 
fclose(fp); 

system( "pause "); 
return 0; 

【转】二进制文件的读写浮点数

原文:http://www.cnblogs.com/wi100sh/p/4835524.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!