Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ equivalent of sprintf?

Tags:

c++

printf

I know that std::cout is the C++ equivalent of printf.

What is the C++ equivalent of sprintf?

like image 773
lital maatuk Avatar asked Feb 13 '11 07:02

lital maatuk


People also ask

What is sprintf in C?

sprintf() in C sprintf stands for "string print". In C programming language, it is a file handling function that is used to send formatted output to the string. Instead of printing on console, sprintf() function stores the output on char buffer that is specified in sprintf.

What is the difference between printf () and sprintf ()?

The only difference between sprintf() and printf() is that sprintf() writes data into a character array, while printf() writes data to stdout, the standard output device.

Does sprintf overwrite C?

The sscanf () function and the sprintf () function are like the two sides of a coin. You can now use the sprintf() function to reassemble the string. You can use the same char array stringa- its previous value gets overwritten. Try it out for yourself to get a better grasp on it.

What is formatted string in C?

The Format specifier is a string used in the formatted input and output functions. The format string determines the format of the input and output. The format string always starts with a '%' character. The commonly used format specifiers in printf() function are: Format specifier.


1 Answers

std::ostringstream

Example:

#include <iostream> #include <sstream> // for ostringstream #include <string>  int main() {   std::string name = "nemo";   int age = 1000;   std::ostringstream out;     out << "name: " << name << ", age: " << age;   std::cout << out.str() << '\n';   return 0; } 

Output:

name: nemo, age: 1000 
like image 63
Vijay Mathew Avatar answered Oct 11 '22 19:10

Vijay Mathew