Dec 12, 2011 at 9:17pm UTC
Hi!
Aren't possible to use seekp() method on an empty stringstream?
I would like make a string, moving freely inside it for store infos, not in order of insertion, using seekp() to determine where to write it...
Dec 12, 2011 at 9:42pm UTC
I'd suggest a std::map<unsigned long, std::string> for that. You can think of the key as the 'position', and iterating the map will go in order of least to greatest.
Last edited on Dec 12, 2011 at 9:43pm UTC
Dec 12, 2011 at 10:47pm UTC
if it's empty, there is nowhere to move with seekp(). But you could make a fixed-size string stream:
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
#include <string>
#include <sstream>
int main()
{
std::ostringstream os(std::string(10, '*' ));
os.seekp(3);
os << 1;
os.seekp(8);
os << 2;
std::cout << os.str() << '\n' ;
}
demo:
http://ideone.com/yvLwv
Granted, it's not the intended use of a string stream, there may be better ways to achieve your goals.
Last edited on Dec 12, 2011 at 10:47pm UTC