is it safe to change the char* returned by ostrstream::str()?

P

Pep

I have inherited a program that does this

ostrstream os;
os << "some text";
os.str()[strlen(os.str()) - 1] = '\0';

In fact is there any point doing it at all?

TIA,
Pep.
 
M

mlimber

Pep said:
I have inherited a program that does this

ostrstream os;
os << "some text";
os.str()[strlen(os.str()) - 1] = '\0';

In fact is there any point doing it at all?

I think the original author was probably trying to add a terminating
zero at the end of the string because the old strstream (not to be
confused with stringstream) classes don't add it automatically. The
code is bad (it doesn't work with VC++ 6). The correct method would be:

os << "some text" << ends;

Cheers! --M
 
P

Pep

mlimber said:
Pep said:
I have inherited a program that does this

ostrstream os;
os << "some text";
os.str()[strlen(os.str()) - 1] = '\0';

In fact is there any point doing it at all?

I think the original author was probably trying to add a terminating
zero at the end of the string because the old strstream (not to be
confused with stringstream) classes don't add it automatically. The
code is bad (it doesn't work with VC++ 6). The correct method would be:

os << "some text" << ends;

Cheers! --M

As I thought.

Cheers,
Pep.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top