What is the output of the below program?
#include <stdio.h> void main () { FILE * filepointer; filepointer = fopen ("new.txt", "r"); if (filepointer == NULL) { printf ("Error opening new file"); exit (1); } else { /* perform desired operations on the file */ } }
-----------------------------------------------
The above code tests two scenarios:
1. Declaring a file pointer
2. Assigning file pointer to a file
2. Assigning file pointer to a file
3. Is a file created by fopen() in 'r'?
4. what is the functionality of exit()?
------------------------------------------------
filepointer is a pointer to a file.
A new.text is tried to be opened and read
fopen() tries to read a file, if the file already exists, it enters "else" loop. If it doesn't exist, it enters the "if" loop
exit() function terminates the program. This helps to exit the execution in case there is any abnormal execution of the code. So, exit() is used in negative cases.
0 Comments