第一题!读入数据有问题!
#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
大家帮帮我。谢谢