0

Text txt.file is argument and has numbers:

154 456 22 3 22
213 312 33 4325

I want to scan numbers from a test.txt and store them into array. This scanning only first 3 numbers. I have no idea why is that so.

int main(int argc,char *argv[])
{
  int c[40];
  int i=0;

  FILE *ptr;
  ptr=fopen(argv[1],"r");
  do
  {
    fscanf(ptr,"%d",&c[i]);

    i++;

  }while(c[i]!=-1);

for(int k=0;k<i;k++) printf("%d ",c[k]);    
  fclose(ptr);    
    return 0;
}

How i can find EOF?

Output is: 154 456 22.

Miko 65
  • 29
  • 1
  • 3
  • 1
    *How i can find EOF?* Read the documentation of `fscanf()`. It returns a value for a reason. – Andrew Henle May 21 '18 at 12:25
  • 1
    You're incrementing `i` *before* checking for `-1`, and besides it's the wrong check. You want to check the *return value* from the function. – unwind May 21 '18 at 12:27
  • [This effort is worth taking it](https://www.google.com/search?q=how+to+detect+EOF+using+scanf+site%3Astackoverflow.com). – alk May 21 '18 at 12:55
  • I would place this line of code insted of your do while loop while(i<40 && EOF != fscanf(ptr,"%d %d %d %d %d",&c[i++],&c[i++],&c[i++],&c[i++],&c[i++])) ; – Aleksa Jovanovic May 21 '18 at 13:35

0 Answers0