Actually, this methodology is not difficult to use, and is extrenely powerful. An example is the best way to show how it works. The following routine is used as a graceful exit from a program when things don't go right. It is used like printf in that you can send a variable amount of data to the function
1st the code:
make sure that you include <stdarg.h>
-
#include <stdarg.h>
-
-
void SysAbort(char *String, int NumArgs, ...)
-
{
-
va_list ap;
-
va_start(ap, NumArgs);
-
-
vfprintf(stderr, String, ap);
-
-
va_end(ap);
-
-
// Check if n\memory needs to be cleaned up
-
MyCleanupRoutine();
-
exit(1);
-
}
This can be called as follows
- SysAbort("Unable to open file %s from path %s", 2, FileName, Path);
The '2' in the call tells the SysAbort function that there are two arguments following, the results would (assuming FileName = "InFile.txt" and Path = ""C:\Data\NewData" look like:
'Unable to open InFile.txt from path C:\Data\NewData'
The same as printf.