Hi everyone. I'm trying to create a function called 'safeprint' which will always check whether the string to be printed actually made it out to the screen. It should be called like this:
int result = safeprint ( __FILE__ , __LINE__ , "Blah blah\n", ... ) ;
if (result < 0 ) // something bad happened
I am doing this partly to make up for my pedantic error checking habit. The problem is that since printf is a variadic function, my function has to be too. How do I make this code work ?
Richard
Code:#include <stdio.h>
#include <string.h>
#include "Error.h"
static const unsigned int StringMax = 100 ;
int safeprint ( const char* file , const unsigned int line , const char* pstring , ... )
{
char buffer [ StringMax ] ;
int ilength = snprintf ( buffer , StringMax , pstring , ... ) ;
int icharsprinted = printf ( "%s" , buffer ) ;
if ( ilength != icharsprinted )
{
ErrorMessage ( "Couldn't print to screen." , file , line ) ;
return -1 ;
}
return 0 ;
}