473,770 Members | 5,862 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

std::stringstre am 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::stringstre am m_szValue;

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

MessageBox::Sho w( m_szValue.str() .c_str() );

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

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

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

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

void
CFusionTag::Set Value( 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::Get Value( char *szValue )
{

MessageBox::Sho w( "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 3314
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::Get Value( char *szValue )
{
memcpy( szValue, m_szValue.str() .c_str(), m_szValue.str() .length() );
}

Jan 9 '08 #2
In article
<32************ *************** *******@e4g2000 hsg.googlegroup s.com>,
TBass <tb*@automatedd esign.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::stringstre am m_szValue;

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

MessageBox::Sho w( m_szValue.str() .c_str() );

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

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

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

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

void
CFusionTag::Set Value( 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::Get Value( char *szValue )
{

MessageBox::Sho w( "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::Get Value() 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...@automated design..comwrot e:
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::Get Value( 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::Get Value(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...@automated design.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::Get Value( 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::Get Value(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<cha rmyvector(500);
mytag->GetValue( &myvector[0] );
Jan 9 '08 #6
TBass <tb*@automatedd esign.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<cha rmyvector(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<cha rmyvector( str.begin(), str.end() );

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

std::vector<cha rmyvector;
mytag->GetValue( back_inserter( myvector ) );
Jan 9 '08 #7
In article
<68************ *************** *******@j78g200 0hsd.googlegrou ps.com>,
TBass <tb*@automatedd esign.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::Get Value( 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::Get Value( 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
7498
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 = "value1" + " : " + "value2";
1
4905
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
16002
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 beginning of the stream as expected. //----------------------------------------------------------------------------- #include <iostream> #include <sstream>
5
12073
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 //----------------------------------------------------------------------------- #include <sstream> #include <iostream> using namespace std;
5
4217
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
3631
by: ma740988 | last post by:
Consider: #include <iostream> #include <sstream> #include <string> int main ( ) { { double pi = 3.141592653589793238; std::stringstream a;
2
21265
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 <iostream.h>
7
3606
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 tho network to a client so that errors and debug messages can be displayed simultaneously anywhere. I tried to use a std::stringstream to do the job. I created the stringstream in main() and pass it as pointer into a few threads. Those threads...
3
5906
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 up into individual elements. The elements can be strings, integers or floating point numbers. In the object where I break the line into elements I use a std::stringstream object to do the actual check:
0
9591
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10001
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9867
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7415
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3573
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.