Option 1: open the file multiple times
This is totally fine. It's not dirty. The operating system and standard library will keep everything straight for you (as long as you're not also writing).
Option 2: read the entire file
This is also totally fine. Most files are small and fit in memory, then you can just use ordinary pointers to point to memory locations.
Option 3: memory map the entire file
On Unix-like systems, you can use mmap()
. This will put the entire file in your address space, but the OS will typically defer actual IO until you read from a particular page in the memory map. This has most of the advantages of options 1 and 2, but it is slightly more complicated to work with, and you would need to write a separate version for Windows, because Windows doesn't have mmap()
(it has something else, I forget what it's called).
Option 4: seek back and forth
You can save your position with ftell()
and then fseek()
later to get back.