简体   繁体   中英

How can I stop reading from a file?

How can I make it stop when I'm end of the file? txt file is look like this;

2 4

5 6

7 8

but it prints

2 x , 4 y

5 x , 6 y

7 x , 8 y

1429697293 x , 4199392 y

int x[20];
int y[20];
FILE *fp;
fp = fopen("coord.txt", "r");


for(int i = 0; i<4; i++){
    if((x[i]=='\0')||(y[i]=='\0')) break;
    fscanf(fp, "%d %d", &x[i],&y[i]);

}

for(int i=0; i<4;i++)
    {
        if((x[i]=='\0')||(y[i]=='\0')) break;
        printf("%d x  , %d y \n",x[i],y[i]);
    }

Alright I just wrote

   while (fscanf(fp, "%d %d", &x[i],&y[i]) != EOF)
   printf("%d x  , %d y \n",x[i],y[i]);

instead of everything and it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM