@Ivan Kushchev , fscanf will not work, tk She reads not in the lines. You can, for example, like this:
int c, nl = 0, skip_lines = сколько строк пропустить; while ((c = fgetc(f)) != EOF) if ((c == '\n') && (++nl == skip_lines)) break; if (feof(f)) fatal("Bad data");
If the size of the lines, or rather the offset of the line of interest in the file is known (or it can be calculated), see man 3 fseek .
Update
@VladD , after getline free is not needed (and if you call free, then the pointer must be reset again).
It is possible so (in one line):
while (n-- && getline(&s, &len, file) > 0); if (n != -1) fatal(...); printf("last skipped line: %s", s);
only eat a little more resources (and generally, I answered, I just forgot about getline).
-
@ Ivan Kushchev , m. in practice, it is better for your task to simply skip lines before the data begins?
while (getline(&s, &len, file) > 0 && *s != 'i');
and then read line by line and use sscanf(s, "%d %d %d %d", ...) to fetch the data.
Update
Yeah, so be it. The idea remains. All the same, because 3 different data blocks.
Just 3 times while (getline(...) && ...); then read (once in a loop).
And if the author does not have getline in libc, then let him write his own version (he will practice at the same time, especially since he may need a dynamic array of structures in the second data block).
nlines? - VladDfgets())? Is it clear how to skip non-numbers in a given string (eg,strcspn())? Is it clear how you can read several numbers separated by spaces (eg,sscanf())? - jfs