How to skip a line when fscanning a text file?

I was able to skip lines with scanf with the following instruction:

fscanf(config_file, "%*[^\n]\n");

The format string matches a line containing any character including spaces. The * in the format string means we are not interested in saving the line, but just in incrementing the file position.

Format string explanation:
% is the character which each scanf format string starts with;
* indicates to not put the found pattern anywhere (typically you save pattern found into parameters after the format string, in this case the parameter is NULL);
[^\n] means any character except newline;
\n means newline;

so the [^\n]\n means a full text line ending with newline.

Reference here.

Leave a Comment