Click here to Skip to main content
15,886,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I want to emulate example printf() like this:

void printfLn( const char *format, ... )
{
	printf( format, ... );
	printf( "\n" );
}


Is there any way to pass the (...) (others) argument to printf?
I make some similar to this years ago an I don't remember how.
Posted
Updated 8-Jun-10 23:05pm
v2

Yes it is possible, but instead of printf you should use vprintf. Look at the code snippet below:

C++
void printfLn(const char *format, ...)
{
   va_list args;
   va_start(args, format);
   vprintf(format, args);
   printf("\n");
   va_end(args);
}


For more details see vprintf documentation[^] and access variable-argument lists[^]
 
Share this answer
 
Comments
ReymonARG 9-Jun-10 12:19pm    
all ok with printf, but I make that like an example.

I have another funcion like this:

set_game_name( const char *format, ... );
An I want to add into my Class so I can make something like this:

MyClass::SetGameName( const char *format, ... )
........set_game_name( format, ... );
Sauro Viti 9-Jun-10 16:06pm    
There are 2 possible scenarios depending on whether or not you have the source code of the set_game_name function that you'd like to "wrap":

1) if you have the source code you can modify the set_game_name function to get a va_list as parameter (i.e. set_game_name(const char *format, va_list args), like vprintf) or you can copy its code inside the body of your class member
2) if you don't have the source code you need to use inline assembler (to do that you must be experienced about C/C++ calling conventions, stack frame layouts, assembler and so on)
ReymonARG 9-Jun-10 17:18pm    
Thanks. I will make with inline ASM. I also know about __cdecl "All push" and then add esp,(4*paramsnum).
Do you have an example?

I was looking for this but I don't test: http://www.codeproject.com/KB/tips/va_pass.aspx (I only see the comments, so no)
Here the same example but a like more: http://wiki.jostudio.net/programmingnote:c:va-pass
ReymonARG 9-Jun-10 18:02pm    
Take a look to this: http://www.ramonberrutti.com/paste/index.php?show=9 with esp give access violation error.
I haven't looked at C++ in years.

But, it sounds like a facade pattern to me. Send the formatting out to different methods within printfLn or aggregate them for combining formats. Then pass that result to the printf() function.

Ryan McBeth
 
Share this answer
 
Take a look at http://en.wikipedia.org/wiki/Stdarg.h[^]. That should get you on your way.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900