write formatted output to a file
#include <stdio.h> #include <stdarg.h> int vfprintf( FILE *fp, const char *format, va_list arg );
The vfprintf() function writes output to the file pointed to by fp, under control of the argument format. The format string is described under the description of the printf() function. The vfprintf() function is equivalent to the fprintf() function, with the variable argument list replaced with arg, which has been initialized by the va_start() macro.
The number of characters written, or a negative value if an output error occurred. If an error occurs, errno indicates the type of error detected.
#include <stdio.h> #include <stdarg.h> FILE *LogFile; /* a general error routine */ void errmsg( char *format, ... ) { va_list arglist; fprintf( stderr, "Error: " ); va_start( arglist, format ); vfprintf( stderr, format, arglist ); va_end( arglist ); if( LogFile != NULL ) { fprintf( LogFile, "Error: " ); va_start( arglist, format ); vfprintf( LogFile, format, arglist ); va_end( arglist ); } } void main() { LogFile = fopen( "error.log", "w" ); errmsg( "%s %d %s", "Failed", 100, "times" ); }
ANSI
Safety: | |
---|---|
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |
_bprintf(), cprintf(), errno, fprintf(), printf(), sprintf(), va_arg(), va_end(), va_start(), _vbprintf(), vcprintf(), vprintf(), vsprintf()