'Rewind a stream, both ifstream as well as strstream

I want to write a HEX file reader. It should support two different formats: Intel HEX and a "ROM" file format where each line contains a pair of address and 16 bit hex value. The reader shall identify the format. For that purpose it must read at least some lines to check if it's at least one valid format.

The file stream shall be passed to the reader with a function like:

class CHexFile { 
public:
    bool Open(std::istream& stream);
    uint16_t GetChunk(); // Return two bytes of the stream.
private:
    void Validate(std::istream& stream);
};

The parameter type is std::istream because this allows to prepare a std::strstream in a unit tester and pass the stream to the function.

It's possible to detect the stream format and validate the entire stream. But how can I rewind the stream after validation (or format detection) and actually retrieve the data?

Note: I have to use a rather old C++ compiler from 2006 (Paradigm). Therefore I can't use fancy features from C++1x or C++2x.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source