Jump to content
  • Advertisement
Sign in to follow this  
discman1028

fstream / loading from memory

This topic is 4479 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been loading from files on disk in my app, and so ifstream has served me well. I would now like to load straight from a buffer (void*), with the size in bytes known as well. I started writing separate functions to do so, but my string parsing code works well with the ifstream class. My question is, is it possible to turn this (void*) buffer in memory to an input fstream, so that I may make minimal changes to my code? If not fstream, what's the standard (C++) way to load from memory? Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
I would try abusing std::istringstream (or std::stringbuf) for this.

Share this post


Link to post
Share on other sites
Actually, the perfect class for this has been deprecated, but you can still use it if you want to if the deprecation isn't a major issue. Use the older strstream. You can initialize it with the memory buffer, and the length of said buffer.

Or build your own streambuf template class that you can initialize and create an istream with it.

Share this post


Link to post
Share on other sites
I ended up using std::stringstream; I could write to the buffer, then read from it as before.

As an aside, I'm a bit confused about the difference between std::stringstream and std::streambuf. Can anyone give an example where each would be useful in mutually exclusive situations?

Share this post


Link to post
Share on other sites
Quote:
Original post by discman1028
I ended up using std::stringstream; I could write to the buffer, then read from it as before.

As an aside, I'm a bit confused about the difference between std::stringstream and std::streambuf. Can anyone give an example where each would be useful in mutually exclusive situations?


The std::*stream classes are formatting classes: they're lightweight wrappers around std::locale facets and std::streambufs and re responsible for converting internal C++ replresentation into serialized stream representation and vice versa.

The std::*buf classes are transport agents resposible for conveying the serialized stream data from a std::*stream to some sink (or from a source to the stream) as appropriate: the stringbuf to memory, the filebuf to a file, etc.

Unlike libraries like the C standard library or the many Java standard libraries, the C++ standard library explicitly separates the serialization of data from the transport of data. I could, for example, replace the streambuf in std::cout with a streambuf wrapping a Berkeley socket and no code that writes to std::cout would have to change.

Share this post


Link to post
Share on other sites
Quote:
Original post by Bregma

The std::*stream classes are formatting classes: they're lightweight wrappers around std::locale facets and std::streambufs and re responsible for converting internal C++ replresentation into serialized stream representation and vice versa.

The std::*buf classes are transport agents resposible for conveying the serialized stream data from a std::*stream to some sink (or from a source to the stream) as appropriate: the stringbuf to memory, the filebuf to a file, etc.

Unlike libraries like the C standard library or the many Java standard libraries, the C++ standard library explicitly separates the serialization of data from the transport of data. I could, for example, replace the streambuf in std::cout with a streambuf wrapping a Berkeley socket and no code that writes to std::cout would have to change.


Thanks - it all makes sense now! :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!