Here my question is calculating the sum of infinite no.of integers where the input is taken from a file.The file contains infinite no.of integers in a line delimited by space.And also I need to display invalid input if those input of integer contain any other characters or symbols.I had tried this code and output well Here is my code....
void main()
{
int i=1,j,a[100000],total=0,r=0;
char discard,buffer[1024];
FILE *fp;
char filename[100];
scanf("%s",filename);
fp=fopen(filename,"r");
do
{
fscanf(fp,"%1024s%c",buffer,&discard);
r+=sscanf(buffer,"%d",&a[i]);
total+=a[i++];
} while(discard!='\n');
if(r==i-1)
{
printf("\n%d",total);
}
else
printf("\n Invalid Input");
}
The code is executing well.But the problem here is the code exceeding my time constraint.Please help me so that i could get a better code