A file handling in c enable use to create, update, read and delete the files stored on the local file system through our c program. File is a sequence of bits, btyes, lines or records whose meaning is defined its creator user i.e. file is a collection of related data that a computers treats as a single unit. When a computer reads a file,it copies the file form the storage device to memory .Whwn it writes to a file ,it transfers data from memory to the storage device. File handling in c programming user files stream as a means of communication between programs and data files.The input stream extracts the data from the files and supplies it to the program. The output stream stores the data into the file supplies by the program.

 

File Handling mode 

 

        Read – r – open a file for reading.

        Write – w – open a file for writing.

        Append – a – open a file for writing in append mode.

 

Read 

You fgetc() function reads a character from the input file referenced by fp. The return value is the character read, or in case of any error, It return EOF. ‘R’ means read a file and ‘R+’ opens a text file for both reading and writing.

 

 Example of read file

#include<stdio.h>

#include<conio.h>

#include<process.h>

#include<stdlib.h>

void main(){

            FILE *f;

            int count=0;

            char c;

            clrscr();

            f=fopen("A1.TXT","r");

            if(f==NULL){

                        printf("Unable to open");

                        getch();

                        exit(0);

            }

            while(1){

                        c=fgetc(f);

                        if(c==EOF){

                                    break;

                        }

                        count++;

                        printf("%c",c);

            }

            printf("\nNumber of character:%d",count);

            fclose(f);

getch();

}

Output

        Open a.txt file

        Number of character in a1.txt file= 6



Write 

The function fputc() writes the character value of the argument c to the output stream referenced by fp. It return the written character written on success otherwise EOF if there is an error.’W’ means open a file and writing and ‘W+’ opens a text file for both reading and writing. It first truncates the file to zero length if it exists, otherwise creates a file if it does not exist.

 

Example of write a file

#include<stdio.h>

#include<conio.h>

#include<process.h>

#include<stdlib.h>

void main(){

            FILE *f;

            char c='A';

            clrscr();

            f=fopen("A.TXT","w");

            if(f==NULL){

                        printf("Unable to open");

                        getch();

                        exit(0);

            }

            fprintf(f,"%c",c);

            fclose(f);

getch();

}

Output

        Write a.txt file



Append in a file

Open a file in append mode. If a file is in append mode, then the file is opened. The content with in the file doesn’t change.’A+’ open for reading and writing appending to file.

 

 

Structure in file handling 

We can read entire structure and write entire structure into a file rather then character by charcter.

FWrite – The FWrite can write your data into a file. It can write the structure into your file the structure can contain multiple property for the one instance of a record.

FRead – The Fread can be read the data from file. It can pic the one instance of structure at a time and display it’s record till the end of file is reached.

 

Example of structure in file handling

#include<stdio.h>

#include<conio.h>

#include<process.h>

#include<stdlib.h>

struct student{

            int roll;

            char name[20];

};

void main(){

            struct student stud;

            FILE *f;

            clrscr();

            f=fopen("record.dat","wb");

            if(f==NULL){

                        printf("Unable to open");

                        getch();

                        exit(0);

            }

            printf("Enter student record:");

            scanf("%d%s",&stud.roll,stud.name);

            fprintf(f,"%d%s",stud.roll,stud.name);

            fclose(f);

            f=fopen("record.dat","rb");

            if(f==NULL){

                        printf("Unable to open");

                        getch();

                        exit(0);

            }

            fscanf(f,"%d%s",&stud.roll,stud.name);

            printf("\nRoll number:%d\nName:%s",stud.roll,stud.name);

            fclose(f);

getch();

}

Output

        Enter student record:11

        hanuman

        Roll number:11

        Name:hanuman