0
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.

mch
  • 9,424
  • 2
  • 28
  • 42
  • Little info on how your file is organized. – sjsam Apr 22 '17 at 12:15
  • Please don't make us ignore extra declarations. When you create an MCVE ([MCVE]) for putting onto SO, make sure there are no extra declarations. It is best to report errors on standard error (use `fprintf(stderr, …)` instead of `printf(…)`). And when you fail to open a file, it is a good idea to report the file name that wasn't opened — then the user doesn't have to guess about which file it is that you're referring to. – Jonathan Leffler Apr 22 '17 at 19:01
  • Note that [`while (!feof(file))` is always wrong](http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong). – Jonathan Leffler Apr 22 '17 at 19:03

1 Answers1

3

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;
}
Sniper
  • 1,428
  • 1
  • 12
  • 28