This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

| Insertable Formatting Submitted by

IMHO, an annoying thing about the win32 MessageBox function is that it doesn't work like printf. If I wanted to display a formatted string, I'd normally write something that goes a bit like this:

        char message[256];
        sprintf (message, "%i blind mice", nBlindMice);
        MessageBox (0, message, "Caption", 0);

One way to get round this, of course, is to make function like MessageBoxf which does the above. However, MessageBox calls are not the only place where it would be handy to be able to use formatted strings. So, the work around I use is to make function which returns the formatted string of whatever you pass to it, like so:

        char *_f (char *fmt, ...)
        {
            static char str[8192];

            va_list argptr;

            va_start(argptr, fmt);
            vsprintf(str, fmt, argptr);
            va_end(argptr);

            return str;
        }

So whenever you have a function which has a string parameter (n.b. 'static' needs to be removed if you're planning to use this function more than once per statement), you can just use this helper function to add string formatting functionality.

        MessageBox (0, _f ("%i blind mice", nBlindMice), "Caption", 0);

Ok, now please visit my website here. Ta very much :)
Eric Reynolds / Remo

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.