Checking the Return Value of a Function
Many C
and C++ runtime library functions have a return value associated
with them for error-checking purposes. For example:
- The
_Rfeov()
function returns 1 if the file has moved from one volume to the next. - The
fopen()
function returns NULL if a file is not opened successfully.
To verify that each runtime library function has completed successfully, a program should check the function return values.
Example:
The following figure shows how to check the return value of the Figure 1. ILE C Source to Check for the Return Value of fopen()
fopen()
function.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fp;
if (( fp = fopen ( "MYLIB/QCSRC(TEST)", "ab" )) == NULL )
{
printf ("Cannot open file QCSRC(TEST)\n");
exit (99);
}
}