关于读取文件字符的问题
Write a program which asks user to enter a filename and then readsnumbers from that file until end of file is reached.
When the end of file is reached program prints the sum of numbers
and the amount of numbers that was read from the file.
codeblocks调试没问题,一运行程序就直接死掉。。
请问哪里错了。。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MY_FILENAME "numbers.txt"
#define LINESIZE 80
int main()
{
FILE *fp;
char *filename="numbers.txt",*fileinput,*fileptr;
int count=0;
int value=0;
double sum=0;
char line[LINESIZE];
printf("Please enter a filename!\n");
scanf("%s",fileinput);
fileptr=strstr(filename,fileinput);
if (fileptr)
{
fp=fopen("numbers.txt","r");
if (fp==NULL)
{
printf("Unable to open %s\n", MY_FILENAME);
}
else
printf("numbers from %s...\n",MY_FILENAME);
while (!feof(fp))
{
if (fgets(line,LINESIZE,fp)!=NULL)
{
if (sscanf(line,"%d",&value)==1)
{
sum+=value;
count++;
printf("Read %d\n",value);
}
else
{
printf("Invalid line\n");
}
}
}
fclose(fp);
printf("the sum of %d numbers from %s is %f\n",count,MY_FILENAME,sum);
}
else
printf("the file doesn't exist");
return 0;
}