Reputation: 5
int main(int argc, char *argv[])
{/*Please ignore extra declarations*/
FILE *fp;
int integers[100];
int value;
int i = -1;
char *addr1;
if ((fp = fopen (argv[1], "r")) == NULL)
{
printf("cant open");
return 1;
}
while (!feof(fp) && fscanf(fp, "%d", &value) && ++i < 100)
integers[i] = value;
fclose (fp);
int k;
k = i;
int arrlen, processes, j, *pint, number;
float p;
arrlen = k;
printf("Size of array is %d\nThe array is:\n", arrlen);
for(i = 0; i < k; i++)
{
printf("%d\t", integers[i]);
}
return 0;
}
I have to read from file into integer array. Also if the file contains any other character apart from integer it should report an error. I was able to do the first part which i have posted above. Please help me with the second part. If the file contains only integers then store it in an array.PS: This is only part of some program. I have to work on the integer array in the program later.
Upvotes: 0
Views: 56
Reputation: 1483
After reading the input, your fscanf
will return 0 if it reads character, which means it wont read until EOF.
Use this after while
but before closing fp
if(!(feof(fp) || i==100))
{
printf("Contains character");
return 1;
}
Upvotes: 3