Scanf does not end

So, I have the following code (C):

char c = getc(in);
if (c == '(')
   ...
if (c == '%')
   ...
if (isdigit(c))
{
   int n;
   ungetc(c, in);
   scanf("%i", &n);
   ...
}

Everything is fine and dandy, when I read the input from stdin, but reading the input from the file, the call scanfdoes not end.

I added code around the call to find out what happens before the call scanf. One such example is

  • c = '0'
  • the character immediately after cis equal)

Is there a buffer after ungetcor something else? What can happen that it works fine when input stdin, but not when its file? (I am not familiar with IO in C).

edit: Should be used fscanf... boy - my face is red.

0
source share
3 answers
  • getc() EOF, , . c int, char, EOF.

  • scanf EOF. , scanf("%i", &n) 1, - n, , .1.

  • in, getc(in), FILE *, scanf stdin.

    fscanf scanf .

+1

scanf() stdin. , . getc() . stdin.

fscanf() .

char c = getc(in);
if (c == '(')
   ...
if (c == '%')
   ...
if (isdigit(c))
{
   int n;
   ungetc(c, in);
   fscanf(in, "%i", &n);
   ...
}
0

in my case there was an infinite loop somewhere below the scanf operator, delete the infinite loop:

unsigned long x,y; 
scanf("%llu%llu",&x,&y);
while(i<=4) {   
if(list[i]>=x && list[i]<=y)        
count++;        
//i++;      
}
0
source

All Articles