|
| |||||||||
| Tags: c function, c language, function, programming, sprintf |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| ||||
| ||||
| 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 |
|
#3
| ||||
| ||||
| 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 |
|
#4
| ||||
| ||||
| 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(); |
|
#5
| ||||
| ||||
| 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. |
|
#6
| |||
| |||
| 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. |
|
#7
| |||
| |||
| 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;
}
} |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 |