Q2.Marks: +2.0UGC NET Paper 2: Computer Sc 23rd August 2024 Shift 1
Arrange the following steps of file handling in C in the correct order:
(A) Close the file
(B) Read from or write to the file
(C) Open the file
(D) Check for error
Choose the correct answer from the options given below:
1.(A), (C), (D), (B)
2.(D), (B), (C), (A)
3.(C), (D), (B), (A)✓ Correct
4.(B), (D), (A), (C)
Solution
The correct answer is (C), (D), (B), (A).
Key Points
Open the file: The first step in file handling is to open the file using the appropriate mode (read, write, etc.). This is done using functions like fopen() in C.
Check for error: After attempting to open the file, it's crucial to check whether the file was successfully opened. This involves checking if the file pointer is NULL.
Read from or write to the file: Once the file is successfully opened and error checking is done, you can perform read or write operations using functions like fread(), fwrite(), fprintf(), fscanf(), etc.
Close the file: Finally, after all the necessary operations are done, the file should be closed using the fclose() function to free up resources.
Thus the correct answer is (C), (D), (B), (A).
Additional Information
File handling is a crucial concept in programming that allows you to store and retrieve data from files. It is widely used for data persistence, configuration management, and logging.
Proper error handling during file operations is essential to ensure data integrity and to handle exceptional conditions gracefully.
Closing the file is important to free up system resources and to ensure that data is correctly written to the file, especially in buffered I/O operations.