I have a variable list function
/* vsprintf example */
#include <stdio.h>
#include <stdarg.h>
void PrintFError (char * format, ...)
{
char buffer[50];
va_list args;
va_start (args, format);
vsprintf (buffer,format, args);
perror (buffer);
va_end (args);
}
int main ()
{
FILE * pFile;
char szFileName[]="myfile.txt";
int firstchar = (int) '#';
pFile = fopen (szFileName,"r");
if (pFile == NULL)
PrintFError ("Error opening '%s'",szFileName);
else
{
// file successfully open
fclose (pFile);
}
return 0;
}
In above example how can we check the received message in "PrintError" function that we are not crossing the buffer size in this example 50 while using "vsprintf" in above example. This should be achieved in portable way.
vprintf(orvfprintfif you want to print on standard error or other files) instead ofvsprintf: it will directly print the error, instead of saving it in a variable.