473,406 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Rewrting over an ostringstream object with seekp(0)

The following simple program overwrites the content of an ostriungstream
object.

#include <iostream>
#include <sstream>

using namespace std;

int main()
{
ostringstream oss;

oss << "0123456789" << ends;
cout << '[' << oss.str() << ']' << endl;

oss.seekp(0);
oss << "toto" << ends;
cout << '[' << oss.str() << ']' << endl;

return 0;
}

In MS-DEV-6, the output was (as I suspected):

[0123456789]
[toto]

In MS-DEV-7.1, the output is (note the extra space):

[0123456789 ]
[toto 56789 ]

May someone tell me what is wrong?
Nov 17 '05 #1
1 1582
You get the extra space because you appended 0 characters. ends is
deprecated now, it's coming from a decade old C++ library, when a string
stream didn't use std::string but a char*. What "<< ends" does it puts a
0 byte to the end of the string, and that looks like a space. For more info:
http://tinyurl.com/69g64

The other problem is that seekp doesn't clear the buffer, it only sets
the write (put) pointer to the beginning. You are partially overwriting
the previous contents.

Your code should be (untested):

ostringstream oss;

oss << "0123456789";
cout << '[' << oss.str() << "]\n";

oss.str(""); // this should clear the stream
oss << "toto";
cout << '[' << oss.str() << "]\n";

BTW, endl has a totally different meaning than "\n". endl also flushes
the buffer, which has performance issues. I wouldn't use endl unless I
explicitly wanted to flush the buffer. It's old habit too, but it's not
necessarily a mistake.

VC++ 6 is wrong, VC++ 7.1 is right in this case. Hope this helps.

Tom
Maitre Bart wrote:
The following simple program overwrites the content of an ostriungstream
object.

#include <iostream>
#include <sstream>

using namespace std;

int main()
{
ostringstream oss;

oss << "0123456789" << ends;
cout << '[' << oss.str() << ']' << endl;

oss.seekp(0);
oss << "toto" << ends;
cout << '[' << oss.str() << ']' << endl;

return 0;
}

In MS-DEV-6, the output was (as I suspected):

[0123456789]
[toto]

In MS-DEV-7.1, the output is (note the extra space):

[0123456789 ]
[toto 56789 ]

May someone tell me what is wrong?

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Als | last post by:
What's the meaning of "freeze" as member of ostringstream class? Is it needed always to call freeze() at the disposal of an ostringstream object? Is it also needed before disposing an istringsteam...
3
by: Mathieu Malaterre | last post by:
Hello, I am trying to write this simple code: std::ostringstream s; s << 1024; std::cout << s.str() << std::endl; s.str(""); // <- problem s << 512; std::cout << s.str() << std::endl;
3
by: Dhruv | last post by:
Hello All, I have the following code: *************************************** int numb=1; std::ostringstream o; o << "1-"; for(i=0;i<1000;i++)
2
by: Bill Sun | last post by:
Hi, I have a quetion about to refresh the ostringstream buffer: like this. ostringstream buffer; buffer << 245; // then the buffer.str() = "245"; ......
3
by: Bob Altman | last post by:
Hi all, Why doesn't the following unmanaged C++ code work as expected: string s; ostringstream strm(s); // This stream should store results in s strm << 25; cout << s << endl; // s...
4
by: Paolo | last post by:
Hi all! I'm using ofstream and seekp to write a file. The problem is I have to write a very big file, more than 4 Gb, but the streamoff that I have to pass to seekp isn't a 64 bit int (it's a...
15
by: iu2 | last post by:
Hi all, can someone help me with this? I'm trying to shorthen some logging function in our project, i.e., instead of LogString("...); use a more convenient log() << "String here " <<...
3
by: whatdoineed2do | last post by:
hi, i have come aross a strange problem with a bit of code that uses a ostringstream to build up a string and i extract the string into a user buf via the sgetn() call instead of via the str()...
7
by: Adrian | last post by:
What is a stringsteam supposed to do when you seek past the end of existing buffer. I can seek past the end of a file stream (my implementation fills the space will nulls but I cannot find if this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.