Formatted strings how?

Hey guys! I've looked all over google and I can't figure out how to do this. I want to know how I can implement formatted strings into functions. You know how you can use fprintf() to create special strings with integers and stuff mixed in? I basically want to do the same thing. So for example, let's say I want a function that takes a formatted string and simply returns it. I know I'd have to start out like this:
1
2
3
4
char *Fstring(const char *format, ...)
{
    //empty
}

But what do I put inside the brackets? =?

I'd love it if anyone could help me with this. Thanks in advance.

SuperSonic
The C way: http://cplusplus.com/reference/clibrary/cstdarg/
The C++11 way: http://en.wikipedia.org/wiki/Variadic_template

To be fair, you are making a design mistake. If you must use format strings, try using something like Boost Format:
http://www.boost.org/doc/libs/release/libs/format/

Or stick with the standard sprintf() function.

It would help us to give a better answer if you could describe your problem set better.
Ok so basically, I'm using a game engine and it has a function called openUrl(const char* URL) that opens URL in your default browser. However, I want to rewrite it so that you could do something like openUrl("www.%s.com", "cplusplus");

Does that make sense? =)
C++ stringstreams (formatted stuff)
1
2
#include <sstream>
#include <string> 
1
2
3
ostringstream ss;
ss << "www." << wherever << ".com";
openUrl( ss.str().c_str() );

C++ strings (concatenations only -- which is all you use in your example)
1
2
3
4
5
string s;
s = "www.";
s += wherever;
s += ".com";
openUrl( s.c_str() );

C sprintf()
 
#include <stdio.h> 
1
2
char s[ MAX_URL_SIZE ]; /* whatever MAX_URL_SIZE may be */
openUrl( sprintf( s, "www.%s.com", wherever ) );

C strings
 
#include <string.h> 
1
2
3
4
5
char s[ MAX_URL_SIZE ];
strcpy( s, "www.";
strcat( s, wherever );
strcat( s, ".com" );
openUrl( s );
Thank you! This is very helpful! =D
Topic archived. No new replies allowed.