C: fscanf - infinite loop when the first character matches

I am trying to parse a text file (CSS) using fscanf and pull out all the statements matching this pattern:

@import "some / file / something.css";

To do this, I have the following loop installed:

FILE *file = fopen(pathToSomeFile, "r");
char *buffer = (char *)malloc(sizeof(char) * 9000);

while(!feof(file))
{
    // %*[^@] : Read and discard all characters up to a '@'
    // %8999[^;] : Read up to 8999 characters starting at '@' to a ';'.
    if(fscanf(file, "%*[^@] %8999[^;]", buffer) == 1)
    {
        // Do stuff with the matching characters here.
        // This code is long and not relevant to the question.
    }
}

This works fine for as long as the VERY FIRST character in the file is not "@". (Literally, one space before the first "@" in the CSS file will make the code run.)

But if the very first character in the CSS file is “@”, then what I see in the debugger is an infinite loop - execution enters the while loop, gets into the fscanf statement, but doesn't enter the “if '(fscanf doesn't work), and then continues through the cycle forever.

, fscanf formatters , , . , ?

.

+3
3

:

  • ( ) 1 <<20 >
  • ( ) 0 (- )
  • 1 8999 ;

, " " .

@include , , ( fgets), @include ( @, sscanf, sscanf(line, "%8999[^;]", buffer)).

@include statemens , , getc, ungetc.

0

scanf, :

  • '@',
  • 8999 ';'

, , '@', .

, - , fscanf , .. " %8999[^;]".

+2

, fscanf . fscanf, fscanf, .

fscanf, ( ), , . , , .

+1

Source: https://habr.com/ru/post/1767323/


All Articles