I made a simple program to read the no of values and then, those values from file and storing them in an array and print the values of array.
#include<stdio.h>
#include<stdlib.h>
void main(){
int i=0,j=0,n,no[n];
FILE *fp=fopen("input.txt","r");
if(fp==NULL)
printf("File input error\n");
else{
fscanf(fp,"%d",&n);
*no=(int *)malloc(n*sizeof(int));
while(i<n){
fscanf(fp,"%d",&no[i]);
printf("%d\t",no[i]);
i++;
}
}
}
My input file was as follows
10 37 21 55 52 68 97 02 00 103 84
and the output I got is
37 21 55 52 68 97 2 0
Why do I see this output?
In your code, you are trying to read n elements without checking for EOF. You should add a condition comparing fscanf with number of scanned elements (in this case 1) and therefore avoid reaching EOF unknowingly.