Write formatted output using a pointer to a list of arguments.
#include <stdio.h> | ||
#include <varargs.h> | Required for UNIX System V compatibility | |
#include <stdarg.h> | Required for ANSI compatibility |
int vfprintf( FILE *stream, const char *format, va_list argptr );
int vprintf( const char *format, va_list argptr );
int vsprintf( char *buffer, const char *format, va_list argptr );
int _vsnprintf( char *buffer, size_t count, const char *format, va_list argptr );
stream | Pointer to FILE structure | |
format | Format control | |
argptr | Pointer to list of arguments | |
buffer | Storage location for output | |
count | Maximum number of bytes |
The vfprintf, vprintf, and vsprintf functions format data and output data to the file specified by stream, to standard output, and to the memory pointed to by buffer, respectively. The _vsnprintf function differs from vsprintf in that it writes not more than count bytes to buffer. These functions are similar to their counterparts fprintf, printf, and sprintf, but each accepts a pointer to a list of arguments instead of an argument list.
The format argument has the same form and function as the format argument for the printf function; see printf for a description of format.
The argptr parameter has type va_list, which is defined in the include files VARARGS.H and STDARG.H. The argptr parameter points to a list of arguments that are converted and output according to the corresponding format specifications in the format.
The return value for vprintf, vsprintf, and _vsnprintf is the number of characters written, not counting the terminating null character. For _vsnprintf, if the number of bytes to write exceeds buffer, then count bytes are written and –1 is returned. If successful, the vfprintf return value is the number of characters written. If an output error occurs, it is a negative value.
vfprintf, vsprintf
Standards:ANSI, UNIX
16-Bit:DOS, QWIN, WIN
32-Bit:DOS32X
vprintf
Standards:ANSI, UNIX
16-Bit:DOS, QWIN
32-Bit:DOS32X
_vsnprintf
Standards:None
16-Bit:DOS, QWIN
32-Bit:DOS32X
fprintf, printf, sprintf, va_arg, va_end, va_start
/* VPRINTF.C shows how to use vprintf functions to write new versions
* of printf. The vsprintf function is used in the example.
*/
#include <stdio.h>
#include <graph.h>
#include <string.h>
#include <stdarg.h>
#include <malloc.h>
int wprintf( short row, short col, short clr, long bclr, char *fmt, ... );
void main( void )
{
short fgd = 0;
long bgd = 0L;
_clearscreen( _GCLEARSCREEN );
_outtext( “Color text example:\n\n” );
/* Loop through 8 background colors. */
for( bgd = 0L; bgd < 8; bgd++ )
{
wprintf( (int)bgd + 3, 1, 7, bgd, “Back: %d Fore:”, bgd );
/* Loop through 16 foreground colors. */
for( fgd = 0; fgd < 16; fgd++ )
wprintf( -1, -1, fgd, -1L, “ %2d ”, fgd );
}
}
/* Full-screen window version of printf that takes row, column, textcolor,
* and background color as its first arguments, followed by normal printf
* format strings (except that \t is not handled). You can specify -1 for
* any of the first arguments to use the current value. The function returns
* the number of characters printed, or a negative number for errors.
*/
int wprintf( short row, short col, short clr, long bclr, char *fmt, ... )
{
struct _rccoord tmppos;
short ret, size;
va_list marker;
char *buffer;
/* It's probably safe to use a buffer length of 512 bytes or five times
* the length of the format string.
*/
size = strlen( fmt );
size = (size > 512) ? 512 : size * 5;
if( (buffer = (char *)malloc( size )) == NULL )
return -1;
/* Set text position. */
tmppos = _gettextposition();
if( row < 1 )
row = tmppos.row;
if( col < 1 )
col = tmppos.col;
_settextposition( row, col );
/* Set foreground and background colors. */
if( clr >= 0 )
_settextcolor( clr );
if( bclr >= 0 )
_setbkcolor( bclr );
/* Write text to a string and output the string. */
va_start( marker, fmt );
ret = vsprintf( buffer, fmt, marker );
va_end( marker );
_outtext( buffer );
free( buffer );
return ret;
}