Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , ,

Sponsored Links



c++ equivalent function to the c-function 'sprintf

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 25-10-2008
Dilbert's Avatar
Member
 
Join Date: Aug 2006
Posts: 196
c++ equivalent function to the c-function 'sprintf

Hi

Is there a c++ equivalent function to the c-function 'sprintf'?

Thnaks
__________________
We endorse perversion and call it an alternative lifestyle
We kill our unborn and call it choice
We neglect to discipline our children and call it building self-esteem
We pollute the air with profanity and pornography and call it freedom of expression

Reply With Quote
  #2  
Old 25-10-2008
Member
 
Join Date: Oct 2008
Posts: 43
Re: c++ equivalent function to the c-function 'sprintf

You can also check out boost::format, which can be easier to work with than stringstream
Reply With Quote
  #3  
Old 25-10-2008
Dilbert's Avatar
Member
 
Join Date: Aug 2006
Posts: 196
Re: c++ equivalent function to the c-function 'sprintf

thank you, that looks much more convenient!
__________________
We endorse perversion and call it an alternative lifestyle
We kill our unborn and call it choice
We neglect to discipline our children and call it building self-esteem
We pollute the air with profanity and pornography and call it freedom of expression

Reply With Quote
  #4  
Old 25-10-2008
Jax.S's Avatar
Member
 
Join Date: Mar 2008
Posts: 52
Re: c++ equivalent function to the c-function 'sprintf

I guess it's rather ::sprintf (with extern "C" { / #include <stdio.h> / }).

But if you want some safety, you'd use ::snprintf, and if you don't want to implement the safety yourself, you could rather use lisp^W std:stringstream.

std:stringstream s; s<<"Hi "<<world<<std::endl;
std::string str =s.str();
const char* cstr=s.c_str();
Reply With Quote
  #5  
Old 25-10-2008
Neel23's Avatar
Member
 
Join Date: Mar 2008
Posts: 252
Re: c++ equivalent function to the c-function 'sprintf

C functions are in the "root" namespace; to avoid
refering a different object in the current namespace, it's advised (in various style guides) to qualify C functions.

Then, using a function without declaring it would make a sane compiler complain, so it's good practice to include some header declaring it before using it.
Reply With Quote
  #6  
Old 25-10-2008
Member
 
Join Date: Oct 2008
Posts: 20
Re: c++ equivalent function to the c-function 'sprintf

The is the C++ group here, so the C header <stdio.h> is to be
included using <cstdio> and its content is accessible only within
the 'std' namespace.
Reply With Quote
  #7  
Old 13-12-2011
Member
 
Join Date: Dec 2011
Posts: 1
Re: c++ equivalent function to the c-function 'sprintf

This is works well, works like a sprintf for c++

Code:
std::string string_format(const std::string &fmt, ...) {
       int n, size=100;
       std::string str;
       va_list ap;
       while (1) {
       str.resize(size);
       va_start(ap, fmt);
       int n = vsnprintf((char *)str.c_str(), size, fmt.c_str(), ap);
       va_end(ap);
       if (n > -1 && n < size)
           return str;
       if (n > -1)
           size=n+1;
       else
           size*=2;
       }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "c++ equivalent function to the c-function 'sprintf"
Thread Thread Starter Forum Replies Last Post
C# Equivalent Of Javascript's Location.replace() Function? ASHER Software Development 6 16-05-2010 02:20 AM
c# function equivalent to gettime function in javascript Omaar Software Development 4 10-03-2010 10:44 PM
How does abstract function differs from virtual function? Maddox G Software Development 5 29-01-2010 11:32 AM
How to implement the INITCAP equivalent function in SQL Server TechPredator Software Development 3 06-08-2009 04:33 PM
Function keys don't function under windows XP GunFighter Hardware Peripherals 3 09-04-2009 12:07 AM


All times are GMT +5.5. The time now is 04:14 AM.