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

What to use instead of sprintf?

Hi,

Currently, whenever I need to work with numbers as strings, I am using
sprintf to do the conversion. This seems a little prehistoric. Is
there a current STL alternative?

One would think that std::string would support it somehow. But things
like:

std::string test = "hello";
test += 99;
std::cout << test;

do not result in "test99" as I would like. Can anyone help me out, or
is something in the printf family of functions still required?

Thanks,
cpp
Jul 22 '05 #1
5 21751
Le 15/06/2004 à 01:16:49, cppaddict <he***@hello.com> a écrit:
Hi, Hi
[...]


$ cat ss.cc
#include <iostream>
#include <sstream>
#include <string>

using std::cout;
using std::endl;
using std::string;
using std::stringstream;

int
main()
{
stringstream ss;

string test = "hello";

ss << test << 99;

cout << ss.str() << endl;

return 0;
}
$ g++ -W -Wall -std=c++98 ss.cc
$ ./a.out
hello99

--
Nomak
Jul 22 '05 #2
Nothing personal, dude, but do you, like, own a C++ tutorial on paper??

cppaddict wrote:
Currently, whenever I need to work with numbers as strings, I am using
sprintf to do the conversion. This seems a little prehistoric. Is
there a current STL alternative?

One would think that std::string would support it somehow. But things
like:

std::string test = "hello";
test += 99;
std::cout << test;


#include <sstream>
....
std::stringstream z;
z << "hello " << 99;
std::cout << z.str();

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #3
main()
{
stringstream ss;

string test = "hello";

ss << test << 99;

cout << ss.str() << endl;

return 0;
}


Yes, sstream. I knew there was something.

thank you Nomak and Philip.
Jul 22 '05 #4
cppaddict wrote:

Hi,

Currently, whenever I need to work with numbers as strings, I am using
sprintf to do the conversion. This seems a little prehistoric. Is
there a current STL alternative?

http://www.parashift.com/c++-faq-lit....html#faq-38.1


Brian Rodenborn
Jul 22 '05 #5
cppaddict <he***@hello.com> writes:
Hi,

Currently, whenever I need to work with numbers as strings, I am using
sprintf to do the conversion. This seems a little prehistoric. Is
there a current STL alternative?

One would think that std::string would support it somehow. But things
like:

std::string test = "hello";
test += 99;
std::cout << test;

do not result in "test99" as I would like. Can anyone help me out, or
is something in the printf family of functions still required?

Thanks,
cpp


Take a look at boost format library.

http://boost.org/libs/format/index.html

--
Regards,
Misha Polatov
MetaCommunications Engineering
Jul 22 '05 #6

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

Similar topics

3
by: huey_jiang | last post by:
Hi All, I am trying to figure out a right syntax to convert an integer array into hex array. sprintf worked for me on doing single integer: int i, Iarray, n=15; char buf; sprintf(buf,...
3
by: jt | last post by:
Here is my code: unsigned long listenip; short tlen; sprintf(tmp,"len:%x listenip:%x",htonl(tlen),listenip); If the value on "tlen" is less than 2 bytes and the value on "listenip" is...
1
by: jimjim | last post by:
Hello, I was wondering about the implications of giving as an argument to sprintf a different data type from the one specified in the format argument. This type of question along with some...
12
by: babak | last post by:
Hi everyone I want to format a string (using sprintf) and put it in a messagebox but however I try to do it, it doesn't seem to work. Here is an example sample of what i try to do: char msg;...
9
by: Neal Barney | last post by:
I have a C program which runs on a device using a Zilog Z180 microprocessor. While it can address 1MB of RAM, it can only address 64KB at any given time. And of that only 16KB can be used for...
5
by: tjay | last post by:
Hi. I wrote some code using sprintf and atof to store a double as a string of fixed length and to convert it back to a double variable. The string is stored in a char buffer global variable. I'm...
3
by: ujjwaltrivedi | last post by:
Can I use any method to allocate /deallocate memory other than new/ delete operators? Actually the problem is while using delete operator in my ".sqc" files the application crashes. It seems the...
2
by: satty888 | last post by:
i've got a bunch of questions for coursework at uni and theres no good notes that i have or been given. can someone please tell me what a sprintf function does if it was used like this: ...
15
by: krister | last post by:
Hello, I'm working in a quite large system that has some limitations. One of those is that I can't use printf() to get an output on a screen. I'm forced to use a special function, let's call it...
3
by: google | last post by:
Consider the following code: char str; char str2; strcpy(str, "%alfa% %beta% d%100%d %gamma% %delta%"); printf("printf: "); printf("1%s2", str); printf("\nsprintf: "); sprintf(str2, "1%s2",...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
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: 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...
1
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.