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

std::stringstream returning back values

Hi.

I wrote a "tag" class to store values. The user gets to store most any
type he would need. Instead of getting too complicatated, I decided I
would store the value as a stringstream, then overload the SetValue
method.

....

protected:
std::stringstream m_szValue;

....
void
CFusionTag::SetValue( char *szValue )
{
m_szValue << szValue;

MessageBox::Show( m_szValue.str().c_str() );

} /* ::SetValue */
void
CFusionTag::SetValue( unsigned int uiValue )
{
m_szValue << uiValue;
}

void
CFusionTag::SetValue( int iValue )
{
m_szValue << iValue;
}

void
CFusionTag::SetValue( float fValue )
{
m_szValue << fValue;
}

void
CFusionTag::SetValue( double dValue )
{
m_szValue << dValue;
}

void
CFusionTag::SetValue( long lValue )
{
m_szValue << lValue;
}
That works fine (I'm aware of the obvious flaw in this code, but I use
taking advantage of that for debugging purposes). The value gets
stored.

My problem (which is probably obvious to everyone but me) is returning
the value when someone calls the GetValue method.

void
CFusionTag::GetValue( char *szValue )
{

MessageBox::Show( "Sending back a char value!" );
szValue = (char *)m_szValue.str().c_str();
}

The char string returned is always blank. I thought this would be
pretty simple, but STL keeps getting the better of me. Can anyone
point me in the right direction?

Thanks in advance,
T
Jan 9 '08 #1
7 3254
I don't know if this would be the "C++" way, but doing it the way I
would do it in C worked.

void
CFusionTag::GetValue( char *szValue )
{
memcpy( szValue, m_szValue.str().c_str(), m_szValue.str().length() );
}

Jan 9 '08 #2
In article
<32**********************************@e4g2000hsg.g ooglegroups.com>,
TBass <tb*@automateddesign.comwrote:
Hi.

I wrote a "tag" class to store values. The user gets to store most any
type he would need. Instead of getting too complicatated, I decided I
would store the value as a stringstream, then overload the SetValue
method.

...

protected:
std::stringstream m_szValue;

...
void
CFusionTag::SetValue( char *szValue )
{
m_szValue << szValue;

MessageBox::Show( m_szValue.str().c_str() );

} /* ::SetValue */
void
CFusionTag::SetValue( unsigned int uiValue )
{
m_szValue << uiValue;
}

void
CFusionTag::SetValue( int iValue )
{
m_szValue << iValue;
}

void
CFusionTag::SetValue( float fValue )
{
m_szValue << fValue;
}

void
CFusionTag::SetValue( double dValue )
{
m_szValue << dValue;
}

void
CFusionTag::SetValue( long lValue )
{
m_szValue << lValue;
}
That works fine (I'm aware of the obvious flaw in this code, but I use
taking advantage of that for debugging purposes). The value gets
stored.

My problem (which is probably obvious to everyone but me) is returning
the value when someone calls the GetValue method.

void
CFusionTag::GetValue( char *szValue )
{

MessageBox::Show( "Sending back a char value!" );
szValue = (char *)m_szValue.str().c_str();
}

The char string returned is always blank.
Your not returning anything. Try this:

std::string
CFusionTag::GetValue() const
{
return m_szValue.str();
}
I thought this would be pretty simple, but STL keeps getting the
better of me. Can anyone point me in the right direction?
Your having problems because you are trying to use C idioms (char*)
instead of C++ idioms (string.)
Jan 9 '08 #3
On 1ÔÂ9ÈÕ, ÉÏÎç10ʱ16·Ö, TBass <t...@automateddesign..comwrote:
I don't know if this would be the "C++" way, but doing it the way I
would do it in C worked.

void
CFusionTag::GetValue( char *szValue )
{
memcpy( szValue, m_szValue.str().c_str(), m_szValue.str().length());

}- Òþ²Ø±»ÒýÓÃÎÄ×Ö -

- ÏÔʾÒý
the folowing is another C way through passing argument by pointer:
void
CFusionTag::GetValue(const char** szValue)
{
if(szValue != NULL)
{
*szValue = m_szValue.str().c_str();
}
}

Jan 9 '08 #4
On Jan 8, 10:36 pm, zhangy...@yahoo.com.cn wrote:
On 1ÔÂ9ÈÕ, ÉÏÎç10ʱ16·Ö, TBass <t...@automateddesign.comwrote:I don't know if this would be the "C++" way, but doing it the way I
would do it in C worked.
void
CFusionTag::GetValue( char *szValue )
{
memcpy( szValue, m_szValue.str().c_str(), m_szValue.str().length() );
Hmmm... Do we have enough space at szValue? Probably not! :(
the folowing is another C way through passing argument by pointer:
void
CFusionTag::GetValue(const char** szValue)
{
if(szValue != NULL)
{
*szValue = m_szValue.str().c_str();
That doesn't work... The std::string value that str() returns is a
temporary object that dies at the semicolon. So, the C-style string
that is returned by c_str() is not valid beyond that point.

Ali
Jan 9 '08 #5
That doesn't work... The std::string value that str() returns is a
temporary object that dies at the semicolon. So, the C-style string
that is returned by c_str() is not valid beyond that point.

Ah ha! That's been my problem. I did not know that it was temporary.
Thanks!
[snip]
I thought this would be pretty simple, but STL keeps getting the
better of me. Can anyone point me in the right direction?
Your having problems because you are trying to use C idioms (char*)
instead of C++ idioms (string.)
[/snip]

The reason for the char * as an argument was because it's actually a
vector that I'm passing into the structure.

std::vector<charmyvector(500);
mytag->GetValue( &myvector[0] );
Jan 9 '08 #6
TBass <tb*@automateddesign.comwrote:
That doesn't work... The std::string value that str() returns is a
temporary object that dies at the semicolon. So, the C-style string
that is returned by c_str() is not valid beyond that point.


Ah ha! That's been my problem. I did not know that it was temporary.
Thanks!
[snip]
I thought this would be pretty simple, but STL keeps getting the
better of me. Can anyone point me in the right direction?

Your having problems because you are trying to use C idioms (char*)
instead of C++ idioms (string.)
[/snip]

The reason for the char * as an argument was because it's actually a
vector that I'm passing into the structure.

std::vector<charmyvector(500);
mytag->GetValue( &myvector[0] );
Creating a buffer and assuming it is big enough (or making it insanely
huge) is a (rather poor) C idiom. Better would be:
string str = mytag->GetValue();
std::vector<charmyvector( str.begin(), str.end() );

Even having the function fill a vector that is provided would be better
than using the raw char*.

std::vector<charmyvector;
mytag->GetValue( back_inserter( myvector ) );
Jan 9 '08 #7
In article
<68**********************************@j78g2000hsd. googlegroups.com>,
TBass <tb*@automateddesign.comwrote:
I don't know if this would be the "C++" way, but doing it the way I
would do it in C worked.

void
CFusionTag::GetValue( char *szValue )
{
memcpy( szValue, m_szValue.str().c_str(), m_szValue.str().length() );
}
What if the block passed in isn't big enough? At the very least, even in
C, you should pass in the size of the block handed over...

void
CFusionTag::GetValue( char* value, int value_size ) {
memcpy( value, m_szValue.str().c_str(),
std::min( value_size, m_szValue.str().length() ) );
}

The fact that you are having to chain the calls (like
"m_szValue.str().c_str()" and "m_szValue.str().length()") means there is
probably an easier way.
Jan 9 '08 #8

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

Similar topics

2
by: Woodster | last post by:
I am using std::stringstream to format a string. How can I clear the stringstream variable I am using to "re use" the same variable? Eg: Using std::string std::string buffer; buffer =...
1
by: KidLogik | last post by:
Hello! I am using std::stringstream && std::string to parse a text file -> std::ifstream in; std::string s; std::streamstring ss; int n; I grab each line like so -> std::getline(in,s);
4
by: Dylan | last post by:
Hi again, In the following program I expected step 3 to assign the values 1 and 2 to iVal1 and iVal2 yet it appears ss.seekg(0, std::ios::beg) does not move the read position back to the...
5
by: Mr Fish | last post by:
Is it possible for me to record floats in a std::stringstream with 100% accuracy? If so how? Here's a little prog that demonstrates how the default ss loses accuracy ...
5
by: Marcin Kalicinski | last post by:
Is there a vectorstream class that implements the functionality similar to std::stringstream but with std::vector, not std::string? cheers, Marcin
5
by: ma740988 | last post by:
Consider: #include <iostream> #include <sstream> #include <string> int main ( ) { { double pi = 3.141592653589793238; std::stringstream a;
2
by: akitoto | last post by:
Hi there, I am using the std::stringstream to create a byte array in memory. But it is not working properly. Can anyone help me? Code: #include <vector> #include <sstream> #include...
7
by: Ziyan | last post by:
I am writing a C/C++ program that runs in background (Linux). Therefore, normally no output would be written into standard output. However, sometimes I want to have debug message collected and sent...
3
by: Rune Allnor | last post by:
Hi all. I am trying to use std::stringstream to validate input from a file. The strategy is to read a line from the file into a std::string and feed this std::string to an object which breaks it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.