If all your numbers are (for example) six characters in length, and you have a single \n
newline character at the end of each line, you can get to line number n
(zero-based) by fseeking to n * 7
. Line 0 is at offset 0, line 1 at offset 7, line 2 at offset 14 and so on.
Similarly, line 1100 (the 1101st line), could be gotten to with something like:
if (fseek (f, 7700L, SEEK_SET) != 0) {
// something went wrong.
}
if (fscanf (f, "%d", &buffer) != 1) {
// something else went wrong.
}
That will work for any fixed width line, you just have to adjust the multiplication factor based on the line width and the line endings (for example, DOS encoding may have two characters, \r\n
).
Of course, you may find it advantageous to just read the entire file into an integer array in memory (depending on how many thousands there are) so that random access to the data becomes blindingly fast - that's because there's no reason to go back to the file for any data after the initial load.