Why these two are not equal?

Viewed 93

The First Code Fragment

fscanf (Fp, "%d",&Variable);
fseek  (Fp, 2, SEEK_CUR); // Ignore <Enter>: 0x0D 0x0A (Windows Platform)
fread  (&String, sizeof(char), Length, Fp);

The Second Code Fragment

fscanf (Fp, "%d\r\n",&Variable); // Ignore <Enter>: 0x0D 0x0A (Windows Platform)
fread  (&String, sizeof(char), Length, Fp);

The Input File

13
    Foobar

The first code can get the four white-blanks before Foobar, but the second can't.

Why? Thanks.

PS: If can, please help me rename the topic to make it more specific. Appreciate your help.

2 Answers

When using fscanf, if you specify one or more whitespace characters in the format string, it will skip any amount of whitespace from the input. It makes no difference which whitespace characters you specified or how many. So in this case when you put \r\n in the format string, it actually skips both the newline sequence and the spaces at the beginning of the next line.

From the documentation of fscanf...

The format string consists of whitespace characters (any single whitespace character in the format string consumes all available consecutive whitespace characters from the input),

http://en.cppreference.com/w/c/io/fscanf

"\n" is a whitespace character so it's consuming all of the white space until the F in "Foobar"

Related