fstream / loading from memory

Started by
6 comments, last by discman1028 17 years, 9 months ago
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.
--== discman1028 ==--
Advertisement
I would try abusing std::istringstream (or std::stringbuf) for this.
Why do you muse w/ "abuse"? (What makes him say "abuse"?)

[Edited by - discman1028 on July 8, 2006 1:50:47 AM]
--== discman1028 ==--
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.
.
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?
--== discman1028 ==--
2005 CRT memory leaks: std::basic_iostream ( affects std::stringstream, std::fstream, probably others ). Just stay alert. ;) Leak is reproducible with trivial code in VC++ 2005 Express.
"after many years of singularity, i'm still searching on the event horizon"
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.

Stephen M. Webb
Professional Free Software Developer

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! :)
--== discman1028 ==--

This topic is closed to new replies.

Advertisement