0

Here is my code

#include<stdio.h>
int main()
{
    FILE *fp;
    int x,y,t,q;
    char ch;
    fp=fopen("sth.xyt","r");
    while ((fscanf(fp,"%d %d %d %d",&x,&y,&t,&q))!=0)
        printf("x=%d y=%d theta=%d quality=%d\n",x,y,t,q);
        ch=getchar();
//  fscanf(fp,"%d %d %d %d",&x,&y,&t,&q);
//  printf("x=%d y=%d theta=%d quality=%d",x,y,t,q);
    fclose(fp);
    return 0;
}

Here sth.xyt has multiple lines.
Each line contains 4 number separated by spaces. example 115 757 180 6
144 191 214 17
170 114 202 6
182 253 236 80
195 377 56 83
The problem is that loop is running infinitely .
Also I used getchar so that when user presses enter key then next line is read from file . That's not happening . Currently my output is
an infinite series of
x=195 y=377 theta=56 quality=83

user1371666
  • 445
  • 1
  • 5
  • 18

1 Answers1

0

Found it from How to find EOF through fscanf?

I should put !=EOF instead of above !=0 in above code.

Still that getchar is not working as it should.

user1371666
  • 445
  • 1
  • 5
  • 18