Pages

File Handling


Introduction of file  
                               File is collection of  similar type of records .you can stroe your data in file  and use it any time .Recoard is collection of data .it  is the detail of data  for example  your admission form name , father name , dob ,marks together are know as record . In one record we cannot mix data of different things .We cannot mix different records in a one file .To maintain different records we need to maintain differents files .

Files are three Type
1 Text File
2 Data File
3 Binary File

1 Text File   these are those file which store the information in the text format .it  store the data into character or strings .The extension of text file is  .txt

2 Data file   these are those file which store the data in different format .These also know a database files .The  extension of these file is .dat

3 Binary File  These files store data in binary form that is zero or one ( 0, 1 ) .These files store data in sequence of bytes  .it is an Encrypted file  so we cannot read it .

File Structure           
                          FILE is inbuilt structure which is used to  create a file.We can create a pointer of the File  structure  .Syntax given below

                     FILE  filepointer  ;

                     FILE *fp;

File handling Function In C

1 fopen( )   These  ( )are those ( ) which can open the file .if the file does not exists then fopen ( ) will create  that file . it has two argument .first argument is name of the file that is to be opened and second argument is mode which file is open

Syntax      fp =fopen( “filename” , mode);

                 fp = fopen (“abc.txt” ,” w”);

they can use the three mode read ,write  and append .

2 fclose ( )  These () is used to close the file .it contain the single argument ,which is the file pointer

Syntax  fclose ( file pointer );

               fclose (fp);

3 fputs ( )      It is used to write a string into the file .This ( ) contains two arguments first is string and second is the file pointer .

Syntax    fputs ( string , file pointer);
                fputs ( str, fp );

4  fgets ( )   This is function is used to read the data from a file in string format .This function contains three arguments .First is string  in which data  is stored second argument is the length of the string and third is the file pointer

Syntax  fgets( string ,length ,file pointer );

5 getc( )  this function is used to read a single characrer from a file it contain a single argument of file pointer

Syntax  variable =getc (file pointer );

6 putc    this function is used to write  a single characrer from a file it contain a two argument of file pointer  first is charcter and second is the file pointer .

Syntax  char  a;
              putc ( a ,fp);

7 getw ( )  it is  used to read an intefer from a file . this function  also contains a single argument ,which is the file pointer 

Syntax  a = getw ( fp) ;

8 putw ( )  it is used to write an integer to a file .this function contain two argument first is the integer variable and second is the file pointer

9 feof ( )  it means the file end of file . this function reads the file till end .

Eg   while (   feof (fp) != -1)

10 fwrite ( )  this function is used to  write the data of a structure  into file .it contain four argument .

Syntax       fwrite (address of structure  , file pointer , starting value , sizeof structure );

11  fread  ( )  this function is also used to read the data froma file .it contain is also four arguments.


Program

#include <stdio.h>
#include <conio.h>
void  main ( )
{
char str[20];

FILE *fp;
clrscr ()
printf (“enter the any string “);
gets(str );
fp=fopen(“deep.txt”,” w”);
fputs (str , fp);
printf ( “data is written into the file :”);
fclose (fp);
getch();


}

0 comments: