You are on page 1of 15

File Handling in C

Lecture 17c
20/3/01


    
m      
›ntroduction
{ Files are places where data can be stored
permanently.
{ Some programs expect the same set of data to
be fed as input every time it is run.
{ Cumbersome.
{ Better if the data are kept in a file, and the
program reads from the file.
{ Programs generating large volumes of output.
{ Difficult to view on the screen.
{ Better to store them in a file for later viewing/
processing


    
m      m
aasic File Operations

{ —pening a file
{ Reading data from a file
{ Writing data to a file
{ Closing a file


    
m      
Opening a File
{ ë file must be Ơopenedơ before it can be used.
FILE *fp;
:
fp = fopen (filename, mode);
{ fp is declared as a pointer to the data type FILE.
{ filename is a string - specifies the name of the file.
{ fopen returns a pointer to the file which is used in all
subsequent file operations.
{ mode is a string which specifies the purpose of
opening the file:
Ơrơ :: open the file for reading only
Ơwơ :: open the file for writing only
Ơaơ :: open the file for appending data to it

    
m      
Contd.

{ Points to note:
{ Several files may be opened at the same
time.
{ For the Ơwơ and Ơaơ modes, if the named file
does not exist, it is automatically created.
{ For the Ơwơ mode, if the named file exists, its
contents will be overwritten.


    
m      
„ amples

FILE *in, *out ;


in = fopen (Ơmydata.datơ, Ơrơ) ;
out = fopen (Ơresult.datơ, Ơwơ);

FILE *empl ;
char filename[25];
scanf (Ơ%sơ, filename);
empl = fopen (filename, Ơrơ) ;

    
m      
Closing a File

{ ëfter all operations on a file have been


completed, it must be closed.
{ Ensures that all file data stored in memory buffers
are properly written to the file.
{ General format: fclose (file_pointer) ;
FILE *xyz ;
xyz = fopen (Ơtestơ, Ơwơ) ;
ƦƦ.
fclose (xyz) ;

    
m      
¦ead/Write Operations on Files

{ àhe simplest file input-output (I/—) function are getc


and putc.
{ getc is used to read a character from a file and return it.
char ch; FILE *fp;
Ʀ..
ch = getc (fp) ;
{ getc will return an end-of-file marker E—F, when the end of the
file has been reached.
{ putc is used to write a character to a file.
char ch; FILE *fp;
ƦƦ
putc (c, fp) ;

    
m      
„ ample :: convert a te t file to
all UPP„¦CAS„

× 

   
 

   


    



   
 !
 ! 
"


    
m      
Contd.

{ We can also use the file versions of scanf


and printf, called fscanf and fprintf.
{ General format:
fscanf (file_pointer, control_string, list) ;
fprintf (file_pointer, control_string, list) ;
{ Examples:
fscanf (fp, Ơ%d %s %fơ, &roll, dept_code, &cgpa) ;
fprintf (out, Ơ\nàhe result is: %dơ, xyz) ;

    
m      
Some Points
{ 2ow to check E—F condition when using
fscanf?
{ Use the function feof
if (feof (fp))
printf (Ơ\n Reached end of fileơ) ;
{ 2ow to check successful open?
{ For opening in Ơrơ mode, the file must exist.
if (fp == NULL)
printf (Ơ\n Unable to open fileơ) ;


    
m      
„ ample
á á á
* +

á   á,-


á    á)'...()/& )
 á  &á )/& 
  á00
 
0$& 
 !"#á 
  á'1 2  .()
 á$%&% 3  á
á  á$%   á
á$  'á&á()'( 


    
m      m
Arguments to main ()
{ Command line arguments are parameters supplied to
a program, when the program is invoked.
cc myfile.c
cc xyz.c -lm
netscape www.mailcity.com
average 10 20 30 40 50
{ 2ow do these parameters get into the program?
{ Every C program has a main function.
{ main can take two arguments conventionally called argc and
argv.
{ Information regarding command line arguments are passed
to the program through argc and argv.

    
m      
„choing the command line
arguments

int main (int argc, char *argv[]) {


int i;
printf (Ơargc = %d\nơ, argc) ;
for (i=0; i<argc; ++i)
printf (Ơargv[%d] = %s\nơ,
i,argv[i]) ;
ù   × #
return 0; $
} %&'(  
%&)( 
%&*(× #

    
m      
„ ample :: convert a te t file to
all  , using command line
arguments
×     %&( +!  × !,

   
    
  %&)( 
  %&*( 


   
 !
 ! 
"


    
m      

You might also like