Put numbers in std::string



 DEVELOP > c-Plus-Plus > Put numbers in std::string

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "TBass"
Date: 18 Dec 2007 07:40:03 AM
Object: Put numbers in std::string
Hi,
I've been switching a C program over to C++, and using std::string for
all the string members of the class.
One question I'm having trouble finding documentation on is how to put
numeric varaible data into a string. For example, in the C program I
did the following:
sprintf( sql, "SELECT * FROM DEVICES WHERE RKEY=\'%d\'", device_id );
But, in C++, I'm trying to do something similar to the following:
std::string sql;
int device_id;
sql = "SELECT * FROM DEVICES WHERE RKEY = ' ".... ?? HOW DO I INSERT
THE VALUE OF device_id?
I'm trying to do things the proper "C++" way.
Thanks in advance,
T
.

User: "Nick Keighley"

Title: Re: Put numbers in std::string 18 Dec 2007 07:54:04 AM
On 18 Dec, 13:40, TBass <t...@automateddesign.com> wrote:

I've been switching a C program over to C++, and using std::string for
all the string members of the class.

One question I'm having trouble finding documentation on is how to put
numeric varaible data into a string. For example, in the C program I
did the following:

sprintf( sql, "SELECT * FROM DEVICES WHERE RKEY=\'%d\'", device_id );

But, in C++, I'm trying to do something similar to the following:

std::string sql;
int device_id;

sql = "SELECT * FROM DEVICES WHERE RKEY = ' ".... ?? HOW DO I INSERT
THE VALUE OF device_id?

I'm trying to do things the proper "C++" way.

#include <sstream>
std::ostringstream sql;
sql << "SELECT * FROM DEVICES WHERE RKEY=\'" << device_id << "\'";
do_sql (sql.str().c_str()); // assuming do_sql() takes a char*
that's from memory so check the documentation
--
Nick Keighley
.
User: "TBass"

Title: Re: Put numbers in std::string 18 Dec 2007 08:29:34 AM
On Dec 18, 7:54 am, Nick Keighley <nick_keighley_nos...@hotmail.com>
wrote:

On 18 Dec, 13:40, TBass <t...@automateddesign.com> wrote:





I've been switching a C program over to C++, and using std::string for
all the string members of the class.


One question I'm having trouble finding documentation on is how to put
numeric varaible data into a string. For example, in the C program I
did the following:


sprintf( sql, "SELECT * FROM DEVICES WHERE RKEY=\'%d\'", device_id );


But, in C++, I'm trying to do something similar to the following:


std::string sql;
int device_id;


sql = "SELECT * FROM DEVICES WHERE RKEY = ' ".... ?? HOW DO I INSERT
THE VALUE OF device_id?


I'm trying to do things the proper "C++" way.


#include <sstream>

std::ostringstream sql;
sql << "SELECT * FROM DEVICES WHERE RKEY=\'" << device_id << "\'";
do_sql (sql.str().c_str()); // assuming do_sql() takes a char*

that's from memory so check the documentation

--
Nick Keighley- Hide quoted text -

- Show quoted text -

Thanks, guys. That's exactly what I need.
Much appreciated,
T
.


User: "Boris"

Title: Re: Put numbers in std::string 18 Dec 2007 09:45:09 AM
On Tue, 18 Dec 2007 15:40:03 +0200, TBass <tbj@automateddesign.com> wrot=
e:

Hi,

I've been switching a C program over to C++, and using std::string for=
all the string members of the class.

One question I'm having trouble finding documentation on is how to put=
numeric varaible data into a string. For example, in the C program I
did the following:

sprintf( sql, "SELECT * FROM DEVICES WHERE RKEY=3D\'%d\'", device_id )=

;


But, in C++, I'm trying to do something similar to the following:

std::string sql;
int device_id;

sql =3D "SELECT * FROM DEVICES WHERE RKEY =3D ' ".... ?? HOW DO I INSE=

RT

THE VALUE OF device_id?

I'm trying to do things the proper "C++" way.

If you are using Boost C++ Libraries already Boost.Format might be an =
option: http://www.boost.org/libs/format/index.html
Your code would look like this then:
std::string sql =3D boost::str(boost::format("SELECT * FROM DEVICES WHER=
E =
RKEY=3D\'%1%\'") % device_id);
Boris
.
User: "Christian Hackl"

Title: Re: Put numbers in std::string 18 Dec 2007 02:14:38 PM
Boris wrote:

On Tue, 18 Dec 2007 15:40:03 +0200, TBass <tbj@automateddesign.com> wrote:


sql = "SELECT * FROM DEVICES WHERE RKEY = ' ".... ?? HOW DO I INSERT
THE VALUE OF device_id?

I'm trying to do things the proper "C++" way.


If you are using Boost C++ Libraries already Boost.Format might be an
option: http://www.boost.org/libs/format/index.html

There's also boost::lexical_cast:
try
{
string sql = "SELECT * FROM DEVICES WHERE RKEY = '" +
lexical_cast<string>(device_id) + "'";
}
catch (bad_lexical_cast &)
{
// error converting device_id to string
}
(By the way, this isn't related to C++, but you may check if your DBMS
allows you to use prepared statements instead of constructing query
strings manually like this.)
--
Christian Hackl
.


User: "Tim Love"

Title: Re: Put numbers in std::string 18 Dec 2007 07:56:29 AM
TBass <tbj@automateddesign.com> writes:

One question I'm having trouble finding documentation on is how to put
numeric varaible data into a string.

Read about stringstream. Once you can do I/O to and from files, I/O to and from strings is very similar.
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER