Error like printf

2010-10-13 printf C exit

Sometimes it is handy to have single function to quit application with an error message. Even better when it accepts printf formatting.

Fortunately it is easy to implement with stdarg library and vfprintf function of stdio.

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

void Error(char * format, ...) {
    va_list args;
    va_start(args, format);
    vfprintf(stderr, format, args);
    va_end(args);
    exit(-1);
}

Example usage:

...
FILE *fin = fopen(argv[1],"r");
if(! fin)
    Error("File \"%s\" could not be opened",argv[1]);