File Handling In C


 File 

A File represents a sequence of bites on the disk where the group of related data is stored.

File is a place on storage disk where a group of related data is stored.Files provides a way to store large volume of data that can be used whenever needed.

Types of File 

C programming language supports two types of files and they are as follows :-

1. Text Files

The file that contains ASCII codes of data like digits, alphabets and symbol is called text file or ASCII file like txt file, doc file etc.

2. Binary Files

The file that contains data in the form of bytes (0's and 1's) is called as binary file. Generally, the binary files are compiled version of text files. like bin file.

File Handling

The process of file handling refers to the method of storing data on the disk in the C program in the form of and output or input that might have been generated while running a C program.

File handling in C enables us to create, update, read and delete the files stored on the local file system through our C program. The operations can be performed on a file are given below :-

1. Creation of the new file / Opening an existing file.
2. Reading from the file.
3. Writing to the file.
4. Closing a file/Deleting the file.

Let us discuss one by one:-

1. Creation of the new file/ Opening and existing file

A file can be created or an existing file can be opened by using fopen() function. It is a predefined function which is defined in the stdio.h headerfiles.

firstly, the  fopen() function searches the file to be opened.If the file does not exist, it will be created newly in the location where the program is stored with the filename. Then it loads the file from the disk and place it into the buffer. The buffer is used to provide efficiency for the read operation. and sets up a character pointer which points to the first character of the file.


Syntax

FILE fopen(const char filename, const char mode);

The fopen function takes two arguments or parameters.

1. The filename (string) : If the file is stored at some specific location, then we must mention the full path at which the file is stored. For example : A file name can be like "C:folder/subfolder/filename.ext".

2. The mode (string) : The mode in which the file is to be opened. In C programming various modes are available which can be used with the fopen() function on the basis of what operation we wants to do on the file.

Example 

FILE *fp;

fp=fopen("student.txt", "w");

In the above example, if the file "student.txt" does not exist, it will be created newly in the location where the program is stored because there is no path if given. If the file is already exists then contents will be deleted and opened as an empty file. Reading operation is not allowed on that file because the file is opened in "w" mode.

If we want to perform reading operation also on that file , then the file should be opened in "w+" mode like 

fp=fopen("student.txt", "w+");

Program Example

#include<stdio.h>
int main()
{
    FILE *fp;
    if(fp=fopen("student.txt", "r"))
    {
        printf("file opened successfully in read mode");
    }
    else
    {
        printf("The file is not exist with that name");
    }
    fclose(fp);
    return 0;
}

We can open the file one of the following modes by using fopen() function :-

  • "w" mode : Open a text file in write mode. If the file exists, its contents are overwritten and if the file does not exist, it will be created.
  • "r" Mode : Opens a text file in read mode. If the file does not exist, fopen() returns NULL. If the file exists, it can be read.
  • "a" Mode : Opens a text file in append mode. If the file does not exist, it will be created. If the file exists, contents can be added to the end of the file.
  • "w+" Mode : Open for both reading and writing. If the file exists, its contents are overwritten, if the file does not exist, it will be created.
  • "r+" Mode : Open for both reading and writing. If the file does not exists,  fopen() returns NULL, if the file exist, the contents can be read.
  • "a+" Mode : Open for both reading and appending. If the file does not exist, it will be created. If the file exists, the contents can be added to the end of the file.
  • "rb" Mode : Opens a binary file in read mode.
  • "wb" Mode : Opens a binary file in write mode.
  • "ab" Mode : Opens a binary file in append mode.

Closing  a File 

Whenever we want to close the file we can use fclose() function. The fclose() function is used to close a file. The file must be closed after all the operations on it.

Syntax 

fclose(FILE *fp);





No comments:

Post a Comment