Filestreams and best practice when reading

Hi
im making a program that decodes a special fileformat
and basically what i need to do is:
1. Read every byte starting from a specific offset
2. Do some processing (basically each byte is an opcode)
3. Once i reach a specific 'stop' byte, stop reading.


I was wondering which approach is better:

1)
Create an ifstream,
open the file,
then loop over each byte (doing the processing i need to do before reading the next byte)
and then when i reach the 'stop' byte: Close the fstream.

2)
Create an ifstream,
open the file,
then copy the content in to some sort of buffer,
close the fstream
and THEN do the processing on the buffer?

Personally i like option 2 better because it seem safer than reading directly off of a file stream.
The problem is that i dont know if the std library has any sort of "buffer class" that i can use that has the ability to:

1. Get / Read a specific amount of bytes from the buffer(sometimes i actually need to read more than one byte at a time - depending on the previous opcode)

2. Tell me where in the file / buffer im currently at - i need this to calculate some offsets between the opcodes (similar to ifstreams tellp() function)
Last edited on
You might be looking for a "memory mapped file". This is generally provided by your operating system. In my experience, it's fast and easy. You point at the file you're interested in, and then you read from it like it's already in some array. Actually fetching from disk is abstracted away, and generally it's fast.

https://en.wikipedia.org/wiki/Memory-mapped_file
Bring a Towel and a brown paper bag and a pillow for screaming.
Last edited on
http://www.cplusplus.com/forum/general/238178/
Look at this and maybe you can help me. The bottom code. It's related to what you're asking for.
Topic archived. No new replies allowed.