How can a program be made to print the name of a source file where an error occurs?
The ANSI C standard includes a predefined macro named _ _FILE_ _ that can be used to insert the current
source code filename in your program. This macro, like the _ _LINE_ _ macro (explained in FAQ V.19), can
be very valuable when it comes to debugging your program and checking for logic errors. For instance, the
following code builds on the example for FAQ V.19 by including the filename as well as the line number
when logic errors are trapped:
source code filename in your program. This macro, like the _ _LINE_ _ macro (explained in FAQ V.19), can
be very valuable when it comes to debugging your program and checking for logic errors. For instance, the
following code builds on the example for FAQ V.19 by including the filename as well as the line number
when logic errors are trapped:
int print_document(char* doc_name, int destination)
{
switch (destination)
{
case TO_FILE:
print_to_file(doc_name);
break;
case TO_SCREEN:
print_preview(doc_name);
break;
case TO_PRINTER:
print_to_printer(doc_name);
break;
default:
printf(“Logic error on line number %d in the file %s!\n”,
_ _LINE_ _, _ _FILE_ _);
exit(1);
}
}
Now, any erroneous values for the destination parameter can be trapped, and the offending source file and
line number can be printed.
Cross Reference:
V.18: What are the standard predefined macros?
V.19: How can a program be made to print the line number where an error occurs?
V.21: How can you tell whether a program was compiled using C versus C++?
V.28: What are the _ _DATE_ _ and _ _TIME_ _ preprocessor commands?
No comments:
Post a Comment