我不太明白这段代码有什么问题,我在运行时不断收到分段错误:11。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a=0;
int sum=0;
FILE* filein=fopen("input(1).txt", "r");
while(fscanf(filein, "%d", &a) > 0)
{
sum+=a;
}
fprintf(filein, "Sum = %dn", sum);
fclose(filein);
return 0;
}
谢谢!
加载文件后将其添加到代码中。如果它从未跳转到 if 语句,则不会找到或加载您的文件。
if (filein != NULL) // Checks if the file was loaded.
{
while (fscanf(filein, "%d", &a) > 0)
{
sum += a;
}
printf("Sum = %dn", sum); // prints to command window.
fclose(filein);
}