第一题!读入数据有问题!

jeffrey_nupt 2007-07-11 10:41:10
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
//#include <unistd.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
float *farray, product;
struct stat buf;
int num,index;

/********************************/
if(stat(argv[1],&buf)) // get the file size
{
printf("The file does not exist\n");
exit(0);
}
num = buf.st_size/sizeof(float);
farray = (float *)malloc(buf.st_size);
FILE *fp=fopen(argv[1],"rb");
fread( farray, sizeof(float), num, fp);
fclose(fp);
/********************************/

/*在这里处理数据*/
for(int i=0; i<num; i++)
printf("%f ",*(farray + 1));


/********************************/
/*输出结果*/
printf("%d\n",num);
printf("乘积=%f 首数的序号=%d\n", product, index);
free(farray);
/********************************/
}


为什么这样运行,打印出来的值都是0啊?
但是打印出来的NUM的值却是对的。
另外,我的数据是这样存放的

1.0 2.0 3.0 4.0 3.0 7.0 5.0

大家帮帮我。谢谢
...全文
132 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
pengzhenwanli 2007-07-12
  • 打赏
  • 举报
回复
你的数据是按照文本方式存放的?应该是二进制格式吧,不然读不正确

568

社区成员

发帖
与我相关
我的任务
社区描述
英特尔® 边缘计算,聚焦于边缘计算、AI、IoT等领域,为开发者提供丰富的开发资源、创新技术、解决方案与行业活动。
社区管理员
  • 英特尔技术社区
  • shere_lin
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧