473,320 Members | 1,900 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,320 software developers and data experts.

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

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.

Sep 20 '05 #1
2 1621
Pep wrote:
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

Sep 20 '05 #2
Pep
mlimber wrote:
Pep wrote:
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.

Sep 22 '05 #3

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

Similar topics

9
by: Charles Prince | last post by:
How do I reuse a ostrstream? So far I have replaced all code that does this "delete <ostrstream>.str()" with "<ostrstream>.freeze(0)"
21
by: Nitin Bhardwaj | last post by:
Hi all, It is said that C++ is a strongly typed language and thus a type-safe language (unlike C). So how does one explain the following behaviour : int main(void) { char *p = NULL; p = "A...
4
by: Luca | last post by:
Hi all, there's a way to convert a float to a char*? I have to do this: char* str = "Object Pos: "; char* str1 = //convert my float value to char*; char* s = strcat(str, str1); DrawText(x, y,...
4
by: goldfita | last post by:
My last post - http://groups.google.com/group/comp.lang.c/browse_thread/thread/eacc2938b4c8ee66/0bdf6ab20a6007d0 I have a little bit more challenging question this time. Suppose I have ...
5
by: lallous | last post by:
Hello I am wrapping a C DLL into C# project. Being a newbie in C#, I wonder how one can map the following: struct X { char *str; int count;
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
9
by: bent | last post by:
Ugh. I am taking a c++ foundations course, and all was well until the moment came where I had to break my prototypes out into .h files. Until this point, all my prototyping has been done with...
7
by: krishna81m | last post by:
In the following code snippet, I created a class samp which has two private variables /char/ and /char*/ which are initialized in the constructor. I create a function /samp input()/ that returns an...
3
by: C++Liliput | last post by:
I have a class of the type class A { private: std::string data; ....... public: const char* toString(); };
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.