FILE HANDLING

FILE HANDLING IN C 

To understand the concept of the file handling, we must understand the following things:-


1)Data :- Raw facts are called data. For Example student name,father's name, address,dob, rollno etc. Each individual  item is called data.

2)Record :  Record is collection of data of same type. It is a detail of data. For Example all things in your admission form 
like as name = Ram, fname=Sham Singh, dob=5-09-1990, rollno=1, marks=89 is known as record and it is record of a single student. In one record data should be same concerned item or thing or person.
In one record we cannot mix data of different things.

Eg:-
name of ram
Roll no of sham

This is wrong. Here Roll No and Name must be of same person   
3)File:- File is collection of records of same type. For example students's records will be placed in one file(Student name,Address, Father's name, phno, rollno, school name).

It has following advantages

1)   Data can be easily stored in compact space updating of data is          very easy. Using the pointers concept when we perform any              changes in a file, all the files which related with that file are 
       automatically updated.

2)   Searching is very easy.

3)   The data in modern file  are very secure. Only the authorized             person can view data.

4)    Files are very reliable because adequate backup checks are                there.

The main above of file handling is to transfer data from primary memory to secondary memory.

Types if Files

There are three types of files, which are:-

1) Text Files
2)  Data Files
3)  Binary Files

1) Text Files:- Text files are those files , which store the information in the text format. It stores the data into character or strings. The extension of text file is .txt.

2) Data Files:- Date files are known database file , which stored the data in different formats. The extension of data files is .dat .

3) Binary Files:- These files store data in binary form. These files store data in sequence of bytes. It is an Encrypted file so we cannot read it.

File Structure :

File is our inbuilt structure, which is used to create a file. We can create a pointer of the file structure. It is known as file declaration  

Syntax

FILE filepointer;

Example 

FILE *p;

Note:- The type name FILE is always written in capital letters.     

File Handling Functions:-

1) fopen() function:- Before using data file  it is necessary to open that file. It can be opened by using fopen() function. The function has two arguments , first is name of file name to be opened , and second is string representing mode which the file is to be opened. As shown in the syntax and example given below:

Syntax :-

fp=fopen("filename","mode");

e.g.

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

File name:- It  specifies the file name where we want to store the data. Like "harsh.txt", here harsh is file name and .txt its extension . It means it is text file.
Modes:- File can be opened in following modes:

a) Read:- In this mode we can read the data from the file. when a file opened in this mode, it is not possible to write the data into the file. we can use "r" as a parameter in fopen function.

b)Write:- If a file in this mode is opened we can write the data into file but it is not possible to read the data from file. A file already exist is opened in write mode, the old content of the file will be deleted and new data can be written in the file. We can use "w" as a parameter in fopen function.

c) Append:- If a file in this mode is opened we can add the data into the file at end of  the file. Parameter used in fopen is "a".

r+ mode:- To open  the file for reading and writing the data.

w+ mode:- To open the file for writing. Data can be read  after writting.

a+ mode:- To open the file, write the data at end of the file. Data can be read after writing.

2) fclose() function:- It is used to close a file. This function contains a single argument, which is the file pointer. As shown in the syntax and example given below:

Syntax:-
fclose(file pointer);

Ex.
fclose(fp);

3) fputs() function :- It is used to write a string into the file. This function contains two arguments first is string and second is the file pointer. As shown in the syntax and example:
Syntax 
fputs(string,file pointer);

Example 
fputs(str,fp);

4) fgets() function :- This function is used to read the data from a file in string format. This function contain three arguments. First is string which data is store second argument length of the string and third the flle pointer (fp). As shown in the syntax and example 

Syntax :-
fgets(string,length,file pointer);

Ex.
fgets(str,40,fb);

5) getc() function:- This function is used to read a character from a file. This function contain a single argument of file pointer. As shown in the syntax and example:

Syntax 
variable=getc(file pointer);
Ex.
char c;
a=getc(fp);

Note :- The character read from the file is assigned to a char variable

6) putc() function:- This function is used to write character to a file. It has two arguments , first is character variable and second is the pointer. As shown in the example :-

Ex.

putc(a,fp);

Note:- 
The value of a char variable 'a' is written in the file.

7) getw() function:- This function is used to read an integer from file. This function also contain single argument, which is the file pointer. As shown in the example

a=getw(fp);

8) putw() function:- This function is used to write an integer to a file. This function contain arguments, first is integer  variable and second is the file pointer. As shown in the example 
putw(a,fp);

9)feof() function:- feof means file end of file. This function reads the file till end. This function return a negative number when we reach at end of the file As shown in example:
while(feof(fp)!=-1)

Write a program to store the data in the file using fputs() function.

#include<stdio.h>
#include<conio.h>
void main()
{
        char str[32];
        FILE *fp;
        clrscr();
        printf("enter any string");
        gets(str);
        fp=fopen("harsh.txt","w");
        fputs(str,fp);
        printf("data is written into file");
        fclose(fp);
        getch();

}

 Write a program to store the data in the file using fgets() function.

#include<stdio.h>
#include<conio.h>
void main()
{
        char str[32],*p;
        FILE *fp;
        clrscr();
        fp=fopen("harsh.txt","r");
        while((p=fgets(str,40,fp))!=0)
       {
               printf("%s",str);
       }
       fclose(fp);
       getch();
}


 Write a program to copy the contents one file into another file. 

#include<stdio.h>
#include<conio.h>
void main()
{
       char str[32],*p;
       FILE *fp,*fp1;
       clrscr();
       fp=fopen("harsh.txt","r");
       fp1=fopen("kapila.txt","w");
      while((p=fgets(str,40,fp))!=0)
     {
              fputs(str,fp1);
      }
     fclose(fp);
     fclose(fp1);
     printf("data is copied");
     getch();
}

Write a program to write a integer data in a binary file.
#include<stdio.h>
#include<conio.h>
void main()
{
         FILE *fp;
         int i;
         clrscr();
         fp=fopen("harsh.dat","w");
         for(i=1;i<=10;i++)
        {
                putw(i,fp);
         }
         printf("data is written");
         fclose(fp);
         getch();
}

Write a program to read a integer data from a binary file.
#include<stdio.h>
#include<conio.h>
void main()
{
         FILE *fp;
         int i;
         clrscr();
         fp=fopen("harsh.dat","r");
         while((i=getw(fp))!=EOF)
         {
                 printf("\n%d",i);
         }
        fclose(fp);
        getch();
}



0 comments:

Post a Comment