How to skip a line when fscanning a text file?

前端 未结 4 628
隐瞒了意图╮
隐瞒了意图╮ 2020-11-30 09:03

I want to scan a file and skip a line of text before reading. I tried:

fscanf(pointer,\"\\n\",&(*struct).test[i][j]);

But this syntax s

4条回答
  •  無奈伤痛
    2020-11-30 09:42

    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.

提交回复
热议问题