Javatpoint Logo
Javatpoint Logo

Fopen() function in C

Introduction:

File handling is a crucial component of many programs in the broad world of C programming. The stdio.h header file contains the fopen() function, which is a basic utility for managing file operations. Developers may easily open, read, write, and manipulate files with the help of fopen(). We will examine the variations of the fopen() function, its syntax, file access modes, error handling, and recommended practices in this extensive manual. You will obtain a complete grasp of how to utilize fopen() in your C programs through illustrative examples and corresponding output.

Syntax of fopen() function

The syntax for the fopen() function is given below:

This method accepts two character type parameters:

file_name:

The name of the file you want to open is represented by the first argument, filename, which is a string. It can either be an absolute path or a relative path depending on where the file is located.

mode_of_operation:

It also refers to a C string and indicates the file access mode. The file access modes for C: are listed below.

"r" -

It examines a file and enables read-only access to the file. If the file is successfully opened, the function fopen() loads it into memory and creates a pointer that points to the file's first character. If fopen() cannot open the file, it returns NULL.

"w" -

It examines a file. The contents of the file are overwritten if one already exists. If the file doesn't already exist, one is made. If the file cannot be opened, it returns NULL. It generates a new file that can only be read when written to.

"a" -

It examines a file. If the file is successfully opened, the function fopen() loads it into memory and creates a reference to the file's last character. If the file doesn't already exist, one is made. If the file cannot be opened, it returns NULL. The file is only opened in order to append (add text to the end of the file).

"r+" -

It examines a file and opens the file so that it can be read and written to. If opened successfully, fopen() sets up a pointer that points to the first character in it and loads the file into memory. If the file cannot be opened, it returns NULL.

"w+" -

It examines a file. The file's contents are overwritten if it already exists. If the file doesn't already exist, one is made. If the file cannot be opened, it returns NULL. The distinction between w and w+ is that the file produced by w+ can also be read.

"a+" -

It examines a file. If the file is successfully opened, the function fopen() loads it into memory and creates a pointer that links to the file's last character. If the file doesn't already exist, one is made. If the file cannot be opened, it returns NULL. Opened for reading and adding (writing at file's end).

Return Value:

If the execution is successful, the function will return a pointer to a FILE; otherwise, it will return NULL.

Let's look at some instances to better understand these modes and how they work:

Example:1

When the following command is executed, a new file with the name "demo_file" and the following contents will be created:

Output:

Welcome to javaTpoint

Example 2:

Now that the file has been opened, we may examine it by running the following code, which will show its contents.

Output:

Welcome to JavaTpoint






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA