473,382 Members | 1,639 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,382 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 21762
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",...
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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.