Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

FILE Handling

C Programming Language File HandlingA file is a place where data is stored or retrieved.
It is present in a secondary storage device like Hard Disk Drive (HDD).
In this topic we are going to learn how to store data into the file or retrieve data from the file using I/O (Input/Output) functions like fgetc(), fputc(), fscanf(), fprintf(), fread(), etc.

C Programming Language Way to Store and Retrieve Data from FileThere are two types of files where data is stored or retrieved.

1) Binary Files
2) Text Files

In Binary Files, data is stored or retrieved in binary form.

C Programming Language Binary FileIn Text Files, data is stored or retrieved in text form.C Programming Language Text File

File opening

A File is opened with the help of library function fopen() provided by C.

Syntax

FILE *fopen(const char *file_name, const char *mode);

This function returns the FILE pointer.
The FILE is a structure provided by C.

Therefore,

FILE *ptr = fopen(const char *file_name, const char *mode);

If there is an error while opening a file, then a NULL value is returned by fopen() function.

Example

FILE *ptr = fopen(“D:\\File\\demo.txt”,”w”);

Above line makes a connection with the file demo.txt present in a folder named FILE inside D drive.
The first argument in fopen() is a path of the file where as second argument is known as a mode.
While opening the file, we need to use the mode.
The mode decides the type of the file operation.

File Closing

The file is closed using a function fclose() provided by C.

Syntax

int fclose(FILE *fp);

If there is an error while closing the file, fclose() function returns EOF (End of File with -1) & returns zero on success.

Types of Mode

C Programming Language Types of Mode in File for File Handling

Leave a reply