Mail Archives: djgpp/1997/04/08/21:11:38
dabrownz AT mindspring DOT com wrote:
>
> How do you center printf text on a DOS screen? I can't find anyway to
> do this other than using tabs and spaces (very annoying). What is the
> 'correct' way to do it? Do I have to use 'cprintf' or some other
> keyword or include another library? I'd would really appriciate any
> help, Thanks.
Here's probably the easiest way to do what you describe:
char line[256];
sprintf( line, format-string, args );
printf( "%*s\n", screen_width / 2 + strlen( line ) / 2, line );
You could put this in a function that could be called just like the regular printf() like so:
#include <stdio.h>
#include <stdarg.h>
#define SCREEN_WIDTH 80
int printf_centered( const char *fmt, ... )
{
char line[256];
va_list v;
va_start( v, fmt );
vsprintf( line, fmt, v );
va_end( v );
return printf( "%*s\n", (int) ( SCREEN_WIDTH / 2 + strlen( line ) / 2 ),
line );
}
If I had some more time I could probably come up with a more elegant way,
but this should be fine for most purposes.
--
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I | mailto:fighteer AT cs DOT com |
| God's final message to His Creation: | http://www.cs.com/fighteer |
| "We apologize for the inconvenience."| Fight against proprietary |
| - Douglas Adams | software - support the FSF!|
---------------------------------------------------------------------
- Raw text -