Syntax of fopen function
The format for the fopen function is provided as follows:
FILE *fopen(const char *file_name, const char *mode_of_operation);
This function receives two parameters of type character.
file_name:
The initial argument, known as filename, signifies the name of the file you wish to access. This can be denoted as a string value and may be either an absolute path or a relative path, depending on the file's location.
modeofoperation:
It also pertains to a C string and specifies the file access mode. The following are the file access modes for C: listed underneath.
"r" -
It inspects a file and grants read-only permissions to access the file. Should the file be opened successfully, the fopen function loads it into memory and establishes a pointer pointing to the initial character of the file. In cases where fopen is unable to open the file, it will return NULL.
"w" -
It inspects a file, overwriting its contents if it already exists. In cases where the file doesn't exist, a new one is created. If the file is inaccessible, it will return NULL. The resulting file is created in a manner that restricts reading access until it has been written to.
"a" -
It inspects a file by utilizing the fopen function. When the file is opened successfully, it is loaded into the memory, establishing a pointer to the last character of the file. In cases where the file does not exist, a new one is generated. However, if the file cannot be opened, a NULL value is returned. Access to the file is strictly for the purpose of appending content, allowing text to be added to the end of the file.
"r+" -
It inspects a file and initiates the opening process to enable both reading from and writing to the file. Upon successful opening, fopen establishes a pointer that points to the initial character in the file and brings the file contents into memory. In case the file opening fails, it returns NULL.
"w+" -
It inspects a file, overwriting its contents if it exists or creating a new one if not. In cases where the file cannot be accessed, a NULL value is returned. The key difference between 'w' and 'w+' is that the file generated by 'w+' allows for both reading and writing.
"a+" -
It inspects a file. When the file is opened successfully, the fopen function reads it into memory and generates a pointer that points to the final character of the file. In cases where the file does not exist, a new one is generated. If opening the file is not possible, it will return NULL. The file is opened for both reading and appending (writing at the end of the file).
Return Value:
If the function execution is successful, it will provide a pointer to a FILE; otherwise, it will return NULL.
Let's explore a few examples to gain a deeper comprehension of these modes and their functionalities:
Example:1
// fopen() is demonstrated in this C program
#include <stdio.h>
#include <stdlib.h>
int main()
{
// demo pointer to FILE
FILE* demo;
// makes a file called "demo_file"
// utilising write-plus mode while accessing files
demo = fopen("demo_file.txt", "w+");
// content to the file is added
fprintf(demo, "%s %s %s", "Welcome",
"to", "Logic Practice");
// closes the file pointed by demo
fclose(demo);
return 0;
}
When running the given command, a fresh file named "demo_file" will be generated, containing the specified content:
Output:
Welcome to javaLogic Practice
Example 2:
Upon opening the file, the next step involves inspecting its contents by executing the code snippet below, which will display the information stored within.
// fopen() is demonstrated in this C program
#include <stdio.h>
int main()
{
// demo pointer to FILE
FILE* demo;
int display;
// makes a file called "demo_file"
// utilising write-plus mode while accessing files
demo = fopen("demo_file.txt", "r");
// extracting each character using a loop
while (1) {
// viewing a file
display = fgetc(demo);
// end-of-file symbol
if (feof(demo))
break;
// showing all characters
printf("%c", display);
}
// closes the file that Demo has specified
fclose(demo);
return 0;
}
Output:
Welcome to Logic Practice