tags:

views:

108

answers:

3

Hello there,

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.

+3  A: 

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.

char buffer[100]
fgets(buffer, 100, pointer);

It works as long as your first line is less than 100 characters long. Otherwise, you must check and loop.

WhirlWind
Thanks this works perfectly !!
ZaZu
+3  A: 

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:

FILE *in = fopen("file.txt","rb");

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 use fgetc:

uchar8 c;
do
  c = fgetc(in);
while (c != '\n')

Finally you should have format specifiers inside your fscanf to actually parse data, like

fscanf(in, "%f", floatVariable);

you can refer here for specifiers.

Jack
Thanks for the reply, I tried using `"rb"` but that didnt work :(Thank you for the link to specifiers, im checking them now.
ZaZu
It shouldn't be "rb" but just "r".
Casey
http://www.cplusplus.com/reference/clibrary/cstdio/fopen/
Casey
+1  A: 

fgets would work here.

#define MAX_LINE_LENGTH 80

char buf[MAX_LINE_LENGTH];

/* skip the first line (pFile is the pointer to your file handle): */
fgets(buf, MAX_LINE_LENGTH, pFile);

/* now you can read the rest of your formatted lines */
Casey