Home Segments Index Top Previous Next

550: Mainline

Generally, when you have a program attempt to open a file, you should also have that program check to see whether its attempt was successful. To arrange for such a check, you can take advantage of the fact that fopen returns a pointer to zero, also known as the null pointer, when it fails to open a file:

FILE* trade_source; 
trade_source = fopen("test.data", "r"); 
/* Test for successful opening */ 
while (! trade_source) { 
 printf ("The attempt to open the test.data file failed!\n"); 
 ... 
} 

Note that when you work on a pointer with the negation operation, the result is 0, of type int, if the pointer is the null pointer; otherwise, the result is 1, of type int.