我是一个 C 初学者我试图编写一个代码来从文件中读取浮点数,从单独的行中,这是我的尝试
#include <stdio.h>
#include<math.h>
int main (void)
{
FILE *fb;
FILE *fp;
fb=fopen("sumsquaresin.txt","r");
fp=fopen("q1out.txt","w");
float x,y,z = 0.0;
int n = 1.0,result;
result =fscanf(fb,"%f",&x);
while(result!=EOF)
{
y=pow(x,2.0);
z+=y;
if(result == EOF)
break;
n++;
}
fprintf(fp,"%d were read\n",n);
fprintf(fp,"The sum of squares is %.2f\n",y);
fclose(fb);
fclose(fp);
return 0;
}
我一直在线收到 NULL 和绿色错误:
result =fscanf(fb,"%f",&x);
错误消息说“线程 EXC_BAD_ACCESS(代码=1,地址=0x68”
任何帮助将不胜感激,谢谢