File I/O in C Programming

File I/O consists various operations: opening, closing, reading, and writing files. Additionally, it encompasses tasks like creating and deleting files. These operations are fundamental in managing data stored in files.

What is File?. Use?

A file is a collection of data stored in a specific format on a computer's storage device. It serves as a permanent or temporary repository for information, allowing data to be organized, accessed, and manipulated by computer programs. Files are used extensively in computing for tasks such as storing documents, programs, multimedia content, configuration settings, and more.

Open File

In order to open the file use 'fopen( )' function.the syntax of opening file is_

Example
FILE *variable_name;
variable_name=fopen("file_name","opening_mode");

Here the 'file_name' is the file we want to open.

Modes of opening file

The given below is the modes of the opening file

modes Meaning
r Open an existing text file for reading purpose.
w Open an file text file for writing purpose if it is not exists then create the file.
a Open a text file in append mode.
r+ Open a text file in both reading and writing mode. The file must exist.
w+ Open a text file in both reading and writing mode. If the file exists, it's truncated first before overwriting. Any old data will be lost. If the file doesn't exist, a new file will be created.
a+ Open a text file for both reading and writing. It creates the file if it does not exist. The reading will start from the beginning but writing can only be appended
rb opens a binary file in reading mode.
rw Open a text binary file in both reading and writing mode. The binary file must exist.
ab Opens a binary file in append mode.
rb+ opens a binary file in both reading and writing mode, and the original content is overwritten if the file exists
wb+ opens a binary file in both reading and writing mode and works similar to the w+ mode for binary files. The file content is deleted first and then new content is added.
ab+ opens a binary file in both reading and appending mode and appends data at the end of the file without overwriting the existing content.

Close file

After opening and perform operation then atlast we close the file using 'fclose( )' function.

Syntax
fclose("variable_name");

Writing on File

'fputc()' is a file handling function in C programming which is used to write a character into a file.It writes a single character at a time in a file and moves the file pointer position to the next address to write the next character.the following is syntax of fputc.

fputc("char some_string",FILE *f);

Here,
some_string : string value
f : File Pointer

'fprintf()' function are used to print data in file instead of output console.the syntax of 'fprintf()' is.

  int fprintf(FILE *f, const char *some_string, ...);

Here,
some_string : string value
f : File Pointer

Example
#include<stdio.h>
int main() {
    FILE *f;
    f = fopen("engineerbro.txt", "w");
    fprintf(f, "Opening of file..........\n");
    fputs("I am fputs function \n", f);
    fclose(f);
    return 0;
}

When this code will run, if 'engineerbro.txt' file is not exists then it creates a new file of same name and writes two lines using two different functions.

Read File

'fgetc' and 'fscanf' are used to read the file.

Syntax of fgetc

fgetc(FILE *f);

'fgetc()' function reads a character from the input file referenced by f.If there is case of error then it return 'EOF'.

'fscanf()' function is used to read formatted input from the file. 'fscanf()' works just like 'scanf()' function but instead of reading data from the standard input it reads the data from the file.

Syntax of fscanf

fscanf(FILE *f, const char *format, ...);