File Handling in C

25
7/17/2019 File Handling in C http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 1/25 CSCI N305: CSCI N305:  C Programming C Programming Copyright Copyright ©2006 ©2006 Department of Computer & Information Science Department of Computer & Information Science File Handling in C File Handling in C

description

Handling files in C

Transcript of File Handling in C

Page 1: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 1/25

CSCI N305:CSCI N305: C Programming C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

File Handling in CFile Handling in C

Page 2: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 2/25

N305: C Programming N305: C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

What is a File?What is a File?

•  A A filefile is a collection of related data that ais a collection of related data that acomputers treats as a single unit.computers treats as a single unit.

• Computers store files to secondary storage soComputers store files to secondary storage sothat the contents of files remain intact when athat the contents of files remain intact when acomputer shuts down.computer shuts down.

• When a computer reads a file, it copies the fileWhen a computer reads a file, it copies the filefrom the storage device to memory; when it writesfrom the storage device to memory; when it writesto a file, it transfers data from memory to theto a file, it transfers data from memory to the

storage device.storage device.• C uses a structure calledC uses a structure called FILEFILE (defined in(defined instdio.hstdio.h to store the attri!utes of a file. to store the attri!utes of a file.

Page 3: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 3/25

N305: C Programming N305: C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

"teps in #rocessing a File"teps in #rocessing a File

$.$. Create the stream via a pointerCreate the stream via a pointervaria!le using thevaria!le using the FILEFILE structure%structure%FILE *p;FILE *p;

&.&. 'pen the file, associating the stream'pen the file, associating the streamname with the file name.name with the file name.

.. )ead or write the data.)ead or write the data.*.*. Close the file.Close the file.

Page 4: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 4/25

N305: C Programming N305: C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

+he !asic file operations are+he !asic file operations are

• fopen open a file specify how its openedfopen open a file specify how its opened(read-write and type (!inary-tet(read-write and type (!inary-tet

• fclose close an opened filefclose close an opened file• fread read from a filefread read from a file• fwrite write to a filefwrite write to a file• fsee/-fsetpos move a file pointer to somewherefsee/-fsetpos move a file pointer to somewhere

in a file.in a file.• ftell-fgetpos tell you where the file pointer isftell-fgetpos tell you where the file pointer islocated.located.

Page 5: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 5/25

N305: C Programming N305: C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

File 'pen 0odesFile 'pen 0odes

from Ta!e "#$ in Forouzan & Gilberg% p '00 

Page 6: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 6/25

N305: C Programming N305: C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

0ore on File 'pen 0odes0ore on File 'pen 0odes

from (igure "#' in Forouzan & Gilberg% p '0$

Page 7: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 7/25

N305: C Programming N305: C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

 Additionally, Additionally, 

• r1 open for reading and writing, startr1 open for reading and writing, start

at !eginningat !eginning

• w1 open for reading and writingw1 open for reading and writing(overwrite file(overwrite file

• a1 open for reading and writinga1 open for reading and writing

(append if file eists(append if file eists

Page 8: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 8/25

N305: C Programming N305: C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

File 'penFile 'pen

• +he file open function (+he file open function (fopenfopen serves servestwo purposes%two purposes% 2 3t ma/es the connection !etween the physical3t ma/es the connection !etween the physical

file and the stream.file and the stream.

 2 3t creates 4a program file structure to store the3t creates 4a program file structure to store theinformation5 C needs to process the file.information5 C needs to process the file.

• "ynta%"ynta%filepointer6filepointer6fopen(“filename”, “mode”);fopen(“filename”, “mode”);

Page 9: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 9/25

N305: C Programming N305: C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

0ore 'n0ore 'n fopenfopen

• +he file mode tells C how the program will+he file mode tells C how the program will

use the file.use the file.

• +he filename indicates the system name+he filename indicates the system name

and location for the file.and location for the file.

• We assign the return value ofWe assign the return value of fopenfopen to ourto our

pointer varia!le%pointer varia!le%sp7ata 6 fopen(408F39:.++5, 4w5;sp7ata 6 fopen(408F39:.++5, 4w5;

sp7ata 6 fopen(4A%<<08F39:.++5, 4w5;sp7ata 6 fopen(4A%<<08F39:.++5, 4w5;

Page 10: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 10/25

N305: C Programming N305: C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

0ore 'n0ore 'n fopenfopen

from (igure "#3 in Forouzan & Gilberg% p 3))

Page 11: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 11/25

N305: C Programming N305: C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

Closing a FileClosing a File

• When we finish with a mode, we needWhen we finish with a mode, we need

to close the file !efore ending theto close the file !efore ending the

program or !eginning another modeprogram or !eginning another modewith that same file.with that same file.

• +o close a file, we use+o close a file, we use fclosefclose andand

the pointer varia!le%the pointer varia!le%fclose(spData);fclose(spData);

Page 12: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 12/25

N305: C Programming N305: C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

fprintf()fprintf()

"ynta%"ynta%

fprintf (fp,=string=,varia!les;fprintf (fp,=string=,varia!les;

:ample%:ample%int i = 12;int i = 12;

float x = 2.356;float x = 2.356;

char ch = !;char ch = !;

F"#$ %fp;F"#$ %fp;

fp=fopen(out.txt''');fp=fopen(out.txt''');

fprintf (fp *+, +f +c* i xfprintf (fp *+, +f +c* i x ch;ch;

Page 13: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 13/25

N305: C Programming N305: C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

f!canf()f!canf()

"ynta%"ynta%

fscanf (fp,=string=,identifiers;fscanf (fp,=string=,identifiers;

:ample%:ample%F39: >fp;F39: >fp;

Fp6fopen(4input.tt5,5r5;Fp6fopen(4input.tt5,5r5;

int i;int i;

fscanf (fp,4d=,i;fscanf (fp,4d=,i;

Page 14: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 14/25

N305: C Programming N305: C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

getc()getc()

"ynta%"ynta%

identifier 6 getc (file pointer;identifier 6 getc (file pointer;

:ample%:ample%F39: >fp;F39: >fp;

fp6fopen(4input.tt5,5r5;fp6fopen(4input.tt5,5r5;

char ch;char ch;ch 6 getc (fp;ch 6 getc (fp;

Page 15: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 15/25

N305: C Programming N305: C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

putc()putc()

write a single character to the outputwrite a single character to the outputfile, pointed to !y fp.file, pointed to !y fp.

:ample%:ample%

F39: >fp;F39: >fp;

char ch;char ch;

putc (ch,fp;putc (ch,fp;

Page 16: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 16/25

N305: C Programming N305: C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

:nd of File:nd of File

• +here are a num!er of ways to test for the endoffile+here are a num!er of ways to test for the endoffilecondition. Another way is to use the value returned !ycondition. Another way is to use the value returned !ythethe fscanf fscanf  function%function%

F39: >fptr$;F39: >fptr$;

int istatus ;int istatus ;istatus 6 fscanf (fptr$, =d=, @var ;istatus 6 fscanf (fptr$, =d=, @var ;

if ( istatus 66 feof(fptr$ if ( istatus 66 feof(fptr$

printf (=:ndoffile encountered.<n5 ;printf (=:ndoffile encountered.<n5 ;BB

Page 17: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 17/25

N305: C Programming N305: C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

)eading and Writing Files)eading and Writing Files

include Dstdio.hEinclude Dstdio.hEint main ( int main (

  F39: >outfile, >infile ;F39: >outfile, >infile ;

  int ! 6 , f ;int ! 6 , f ;

  float a 6 $.G&, c 6 H.HI, e, g ;float a 6 $.G&, c 6 H.HI, e, g ;  outfile 6 fopen (=testdata=, =w= ;outfile 6 fopen (=testdata=, =w= ;

  fprintf (outfile, 4 f d f =, a, !, c ;fprintf (outfile, 4 f d f =, a, !, c ;

  fclose (outfile ;fclose (outfile ;

  infile 6 fopen (=testdata=, =r= ;infile 6 fopen (=testdata=, =r= ;

  fscanf (infile,=f d f=, @e, @f, @g ;fscanf (infile,=f d f=, @e, @f, @g ;

  printf (4 f d f <n =, a, !, c ;printf (4 f d f <n =, a, !, c ;  printf (4 f d f <n =, e, f, g ;printf (4 f d f <n =, e, f, g ;BB

Page 18: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 18/25

N305: C Programming N305: C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

:ample:ample

include Dstdio.hEinclude Dstdio.hE

includeDconio.hEincludeDconio.hE

void main(void main(

char ch;char ch;

F39: >fp;F39: >fp;fp6fopen(=out.tt=,=r=;fp6fopen(=out.tt=,=r=;

while(Jfeof(fpwhile(Jfeof(fp

ch6getc(fp;ch6getc(fp;

printf(=<nc=,ch;printf(=<nc=,ch;

BB

getch(;getch(;

 BB

Page 19: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 19/25

N305: C Programming N305: C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

frea* frea*  ((

7eclaration%7eclaration%

  siKeLt fread(void >ptr, siKeLt siKe, siKeLt n, F39: >stream;siKeLt fread(void >ptr, siKeLt siKe, siKeLt n, F39: >stream;

 )emar/s%)emar/s%

fread reads a specified num!er of eMualsiKedfread reads a specified num!er of eMualsiKeddata items from an input stream into a !loc/.data items from an input stream into a !loc/.

  ptr 6 #oints to a !loc/ into which data is readptr 6 #oints to a !loc/ into which data is read

  siKe 6 9ength of each item read, in !ytessiKe 6 9ength of each item read, in !ytes  n 6 Num!er of items readn 6 Num!er of items read

  stream 6 file pointer stream 6 file pointer 

Page 20: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 20/25

N305: C Programming N305: C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

+,amp!e+,amp!e

:ample%:ample%

include Dstdio.hEinclude Dstdio.hE

int main(int main(

F39: >f;F39: >f;

char !ufferO$$P;char !ufferO$$P;  if (f 6 fopen(=fred.tt=, 4r5if (f 6 fopen(=fred.tt=, 4r5

fread(!uffer, $, $Q, f;fread(!uffer, $, $Q, f;

!ufferO$QP 6 Q;!ufferO$QP 6 Q;

fclose(f;fclose(f;

printf(=first $Q characters of the file%<ns<n=, !uffer;printf(=first $Q characters of the file%<ns<n=, !uffer; BB

return Q;return Q;

BB 

Page 21: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 21/25

N305: C Programming N305: C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

fwrite(fwrite(

7eclaration%7eclaration%

  siKeLt fwrite(const void >ptr, siKeLt siKe, siKeLt n, F39:>stream;siKeLt fwrite(const void >ptr, siKeLt siKe, siKeLt n, F39:>stream;

 )emar/s%)emar/s%

fwrite appends a specified num!er of eMualsiKed data items to anfwrite appends a specified num!er of eMualsiKed data items to anoutput file.output file.

  ptr 6 #ointer to any o!Rect; the data written !egins at ptr ptr 6 #ointer to any o!Rect; the data written !egins at ptr 

  siKe 6 9ength of each item of datasiKe 6 9ength of each item of data

  n 6Num!er of data items to !e appendedn 6Num!er of data items to !e appended

  stream 6 file pointer stream 6 file pointer 

Page 22: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 22/25

N305: C Programming N305: C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

:ample:ample

:ample%:ample%include Dstdio.hEinclude Dstdio.hE

int main(int main(

char aO$QP6S$S,S&S,SS,S*S,SS,SHS,SGS,SIS,STS,SaSB;char aO$QP6S$S,S&S,SS,S*S,SS,SHS,SGS,SIS,STS,SaSB;

F39: >fs;F39: >fs;

fs6fopen(=#roRect.tt=,=w=;fs6fopen(=#roRect.tt=,=w=;

fwrite(a,$,$Q,fs;fwrite(a,$,$Q,fs;

fclose(fs;fclose(fs;

return Q;return Q;

BB

Page 23: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 23/25

N305: C Programming N305: C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

fsee/(fsee/(

-hi! function !et! the file po!ition in,icator for the !trea pointe, to b/ !trea-hi! function !et! the file po!ition in,icator for the !trea pointe, to b/ !treaor /ou can !a/ it !ee0! a !pecifie, place ithin a file an, o,if/ it.or /ou can !a/ it !ee0! a !pecifie, place ithin a file an, o,if/ it.  

$$$-$$$- ee0! fro beginning of fileee0! fro beginning of file

 $$4$$4 ee0! fro current po!itionee0! fro current po!ition

 $$$78$$$78 ee0! fro en, of fileee0! fro en, of file

$xaple9$xaple9

:inclu,e:inclu,e Dstdio.hEDstdio.hEintint ainain((

   F39: > f;  F39: > f;  f 6 fopen(=myfile.tt=, =w=;  f 6 fopen(=myfile.tt=, =w=;  fputs(=Uello World=, f;  fputs(=Uello World=, f;  fsee/(f, H, "::VL":+; "::VLC), "::VL:N7  fsee/(f, H, "::VL":+; "::VLC), "::VL:N7

  fputs(= 3ndia=, f;  fputs(= 3ndia=, f;  fclose(f;  fclose(f;  returnreturn Q;Q;BB

Page 24: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 24/25

N305: C Programming N305: C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

ftell(ftell( 

offset 6 ftell( file pointer ;offset 6 ftell( file pointer ;

=ftell= returns the current position for input or output on the file=ftell= returns the current position for input or output on the file

include Dstdio.hEinclude Dstdio.hE

int main(voidint main(void

  F39: >stream;F39: >stream;

  stream 6 fopen(=08F39:.++=, =w=;stream 6 fopen(=08F39:.++=, =w=;

  fprintf(stream, =+his is a test=;fprintf(stream, =+his is a test=;

  printf(=+he file pointer is at !yte ld<n=, ftell(stream;printf(=+he file pointer is at !yte ld<n=, ftell(stream;

  fclose(stream;fclose(stream;  return Q;return Q;

BB

Page 25: File Handling in C

7/17/2019 File Handling in C

http://slidepdf.com/reader/full/file-handling-in-c-568d3c6a1679a 25/25

N305: C Programming N305: C Programming 

CopyrightCopyright ©2006©2006 Department of Computer & Information ScienceDepartment of Computer & Information Science

+UANV 8'+UANV 8'