Testing for Successful Open
{"Image DocumentName": "Testing for Successful Open", "Image ImageDescription": "Engineering H192. Winter Testing for Successful Open. If the file was not able to be opened, then the value returned by the fopen routine is NULL. For example, let s assume that the file mydata does not exist. Then: FILE  fptr1 ; fptr1 = fopen ( mydata , r ) ; if (fptr1 == NULL) { printf ( File mydata did not open.\\n ) ; } Instructor: A good way to check for fault tolerance is to check for the file pointer to be equal to NULL after attempting to open it. If the file pointer is NULL the program should perform some alternate action besides attempting to use the file pointer (such as displaying an for the user). Narrator: Just because you attempt to open a file doesn\u2019t guarantee it will be opened successfully. Various problems may occur such as the file is in use, your program doesn\u2019t have permission to open the file, or the file simply doesn\u2019t exist or is in another directory. But there is a method of testing for a successful file open. In the given example we will attempt to open a file that doesn\u2019t exist. Notice after we attempt to open the file with fopen we then test the file pointer for a value called NULL. If the file pointer contains NULL then the file in fact didn\u2019t open. Otherwise it can be assumed the file opened correctly and is ready for use. Lecture 07.", "Image XResolution": "37", "Image YResolution": "37", "Image ResolutionUnit": "Pixels/Centimeter", "Image Software": "SlidePlayer", "Image YCbCrPositioning": "Centered"}