|
-
Apr 23rd, 2002, 11:53 PM
#1
Thread Starter
Frenzied Member
ostrstream problems
how do you clear or empty an ostrstream?
Code:
#include <windows.h>
#include <strstream>
using namespace std;
void main(){
ostrstream oss;
oss << "3.14159265359" << ends;
MessageBox(0, oss.str(), "", 0);
// now clear out the stream so a new stream can be written
oss << "1.41421356237" << ends;
MessageBox(0, oss.str(), "", 0);
}
-
Apr 24th, 2002, 12:32 PM
#2
Monday Morning Lunatic
Code:
#include <iostream>
#include <sstream>
using namespace std;
int main() {
ostringstream oss;
oss << "Hi there: " << 5 << " " << 10 << ends;
cout << oss.str() << endl;
oss.seekp(0);
oss << "Again!! " << "blah: " << 5 * 10 << ends;
cout << oss.str() << endl;
return 0;
}
....with small modifications to bring you up to the standard
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 24th, 2002, 01:21 PM
#3
Thread Starter
Frenzied Member
Oh, right. stringstreams are the new standard.
-
Apr 24th, 2002, 01:23 PM
#4
Monday Morning Lunatic
Less memory-leak potential since they return a string, not char* (in the old ones, when you call .str(), it locks the buffer and strange things happen).
Plus, these are all templated (w00t!).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 24th, 2002, 01:25 PM
#5
Thread Starter
Frenzied Member
So are you saying oss.str().c_str() may cause a memory leak too?
-
Apr 24th, 2002, 01:28 PM
#6
Monday Morning Lunatic
No, because it returns a constant pointer, and doesn't lock any buffers since it will delete it when the destructor is called.
Hence why you should use the new ones - as Bjarne says, "Resource Initialisation is Acquisition"
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 24th, 2002, 01:30 PM
#7
Thread Starter
Frenzied Member
-
Apr 25th, 2002, 07:47 PM
#8
Thread Starter
Frenzied Member
-
Apr 26th, 2002, 09:42 AM
#9
What does that mean in easy English parksie?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Apr 26th, 2002, 06:04 PM
#10
Monday Morning Lunatic
Originally posted by CornedBee
What does that mean in easy English parksie?
What, the acquisition thing?
It basically means that once you create a representation of something, you have allocated what it is, for example:
Code:
class mutex { };
void lockedcode() {
mutex m; // blocks until it can recieve the mutex
// some unsafe operation
}
In this code, creating the mutex object tries to acquire the OS's mutex handle, and destroying it releases it again.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 27th, 2002, 05:48 AM
#11
Ok, I think I understand...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|