I am using C and my knowledge is very basic. I want to scan a file and get the contents after the first or second line only ...
I tried :
fscanf(pointer,"\n",&(*struct).test[i][j]);
But this syntax simply starts from the first line =\
How is this possible ?
Thanks.
It's not clear what are you trying to store your data into so it's not easy to guess an answer, by the way you could just skip bytes until you go over a
\n
:Then you can either skip a whole line with
fgets
but it is unsafe (because you will need to estimate the length of the line a priori), otherwise usefgetc
:Finally you should have format specifiers inside your
fscanf
to actually parse data, likeyou can refer here for specifiers.
fgets will get one line, and set the file pointer starting at the next line. Then, you can start reading what you wish after that first line.
It works as long as your first line is less than 100 characters long. Otherwise, you must check and loop.
fgets would work here.
I was able to skip lines with scanf with the following instruction:
the format string represent a line containing any character including spaces. The * in the format string and the NULL pointer mean 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.