views:

309

answers:

3

The problems:

Is there a free implementation of printf/sprintf for the .net framework? Other than the above link, I couldn't find anything.

Thanks!

Update:

Thanks for the help, even though you couldn't find anything. That means I'll just have to do it myself (I was trying to avoid it, but oh well...)
I cooked up a sprintf function that supports basic format strings, you can find it here: https://sourceforge.net/projects/printfnet/. I'll try to make it a complete implementation if I can.

+2  A: 

How about a managed C++ wrapper? Pretty sure you can call printf from there.

No Refunds No Returns
I target Silverlight too, so I can't call native functions.
Hali
+2  A: 

I think you want this: http://www.codeproject.com/KB/printing/PrintfImplementationinCS.aspx

It's a free implementation of a port of the C printf function to C#. You should be aware the author points out that not all features of printf are currently supported - but this may be a good starting point.

EDIT: I see that the license for that version isn't compatible with what you need - in that case, I definitely recommend looking at calling the unmanaged version directly as the following blog article discusses. It's probably the most compatible and safest thing to do.

If that doesn't cut it, here's a blog article about actually calling the unmanaged printf function:

http://community.bartdesmet.net/blogs/bart/archive/2006/09/28/4473.aspx

It looks like this is all you need to call the unmanaged printf from C#:

[DllImport("msvcrt40.dll")]
public static extern int printf(string format, __arglist);

static void Main(string[] args)
{
   printf("Hello %s!\n", __arglist("Bart"));
}
LBushkin
Be aware, typing __arglist into VS2008/2010 can crash the IDE. MS wont fix till next major version...
leppie
Thanks, but I target Silverlight too, so I can't call native functions.
Hali
+3  A: 

Why don't you find a GPL-compatible implementation of printf written in C and port it to .NET?

Adam Maras
If LGPL is good enough, the source code of the glibc is here: http://ftp.gnu.org/gnu/glibc/ - stdio-common contains the printf.c which calls the vprintf function in vfprintf.c. Pretty hairy code though...
Michael Stum
Absolutely, not to rule out this idea in total, but the couple of implementations I have seen back in the days were more than unsuitable to be ported to C# - well at least if you care for code ;-) It might as well be easier to find an existing test suite and somehow program against that from scratch.
Christian.K