#include <stdio.h>
int main() {
FILE *filePointer;
// Step 1: Declare a file pointer variable
// FILE is a structure defined in the stdio.h header file
// It represents a file stream
// *filePointer is a pointer to a FILE structure
// Step 2: Open the file using fopen() function
// Syntax: fopen("file_path", "mode")
// "file_path" is the path to the file you want to open
// "mode" specifies the mode in which the file should be opened (read, write, etc.)
filePointer = fopen("example.txt", "r");
// Step 3: Check if the file is successfully opened
// fopen() returns NULL if the file cannot be opened
if (filePointer == NULL) {
printf("Error opening the file.\n");
return 1; // Return an error code
}
// Step 4: File operations can be performed here
// ...
// Step 5: Close the file using fclose() when done
// Syntax: fclose(file_pointer)
// This is important to free up system resources
fclose(filePointer);
return 0; // Return success code
}