Vous êtes sur la page 1sur 26

File Handling in C

What is a File?
A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary storage so that the contents of files remain intact when a computer shuts down. When a computer reads a file, it copies the file from the storage device to memory; when it writes to a file, it transfers data from memory to the storage device. C uses a structure called FILE (defined in stdio.h to store the attri!utes of a file.

"teps in #rocessing a File


$. Create the stream via a pointer varia!le using the FILE structure% FILE *p; &. 'pen the file, associating the stream name with the file name. (. )ead or write the data. *. Close the file.

+he !asic file operations are


fopen , open a file, specify how its opened (read-write and type (!inary-te.t fclose , close an opened file fread , read from a file fwrite , write to a file fsee/-fsetpos , move a file pointer to somewhere in a file. ftell-fgetpos , tell you where the file pointer is located.

File 'pen 0odes

from Table 7-1 in Forouzan & Gilberg, p. 400

0ore on File 'pen 0odes

from Figure 7-4 in Forouzan & Gilberg, p. 401

Additionally,
r1 , open for reading and writing, start at !eginning w1 , open for reading and writing (overwrite file a1 , open for reading and writing (append if file e.ists

File 'pen
+he file open function (fopen serves two purposes%
2 3t ma/es the connection !etween the physical file and the stream. 2 3t creates 4a program file structure to store the information5 C needs to process the file.

"ynta.%

filepointer6fopen(filename, mode);

0ore 'n fopen


+he file mode tells C how the program will use the file. +he filename indicates the system name and location for the file. We assign the return value of fopen to our pointer varia!le%
sp7ata 6 fopen(408F39:.+;+5, 4w5 ; sp7ata 6 fopen(4A%<<08F39:.+;+5, 4w5 ;

0ore 'n fopen

from Figure 7-3 in Forouzan & Gilberg, p. 399

Closing a File
When we finish with a mode, we need to close the file !efore ending the program or !eginning another mode with that same file. +o close a file, we use fclose and the pointer varia!le% fclose(spData);

fprintf()
"ynta.% fprintf (fp,=string=,varia!les ; :.ample%
int i = 12; float x = 2.356; char ch = ! ; F"#$ %fp; fp=fopen(&out.txt'(')'); fprintf (fp( *+, +f +c*( i( x( ch ;

f!canf()
"ynta.% fscanf (fp,=string=,identifiers ; :.ample%
F39: >fp; Fp6fopen(4input.t.t5,5r5 ; int i; fscanf (fp,4?d=,@i ;

fgetc()
"ynta.% identifier 6 getc (file pointer ; :.ample% F39: >fp; fp6fopen(4input.t.t5,5r5 ; char ch; ch 6 fgetc (fp ;

fputc()
write a single character to the output file, pointed to !y fp. :.ample% F39: >fp; char ch; fputc (ch,fp ;

:nd of File
+here are a num!er of ways to test for the end,of,file condition. Another way is to use the value returned !y the fscanf function% F39: >fptr$; int istatus ; istatus 6 fscanf (fptr$, =?d=, @var ; if ( istatus 66 feof(fptr$ A printf (=:nd,of,file encountered.<n5 ; B

)eading and Writing Files


Cinclude Dstdio.hE int main ( A F39: >outfile, >infile ; int ! 6 F, f ; float a 6 $(.G&, c 6 H.HI, e, g ; outfile 6 fopen (=testdata=, =w= ; fprintf (outfile, 4 ?f ?d ?f =, a, !, c ; fclose (outfile ; infile 6 fopen (=testdata=, =r= ; fscanf (infile,=?f ?d ?f=, @e, @f, @g ; printf (4 ?f ?d ?f <n =, a, !, c ; printf (4 ?f ?d ?f <n =, e, f, g ;
B

:.ample
Cinclude Dstdio.hE CincludeDconio.hE void main( A char ch; F39: >fp; fp6fopen(=out.t.t=,=r= ; while(Jfeof(fp A ch6getc(fp ; printf(=<n?c=,ch ; B getch( ; B

freadK(
7eclaration% siLeMt fread(void >ptr, siLeMt siLe, siLeMt n, F39: >stream ; )emar/s% fread reads a specified num!er of eNual,siLed data items from an input stream into a !loc/. ptr 6 #oints to a !loc/ into which data is read siLe 6 9ength of each item read, in !ytes n 6 Oum!er of items read stream 6 file pointer

Example
:.ample% Cinclude Dstdio.hE K int main( A F39: >f; K char !ufferP$$Q; if (f 6 fopen(=fred.t.t=, 4r5 A fread(!uffer, $, $R, f ; !ufferP$RQ 6 R; fclose(f ; printf(=first $R characters of the file%<n?s<n=, !uffer ; BK return R; B

fwrite(
7eclaration% siLeMt fwrite(const void >ptr, siLeMt siLe, siLeMt n, F39:>stream ; )emar/s% fwrite appends a specified num!er of eNual,siLed data items to an output file. ptr 6 #ointer to any o!Sect; the data written !egins at ptr siLe 6 9ength of each item of data n 6Oum!er of data items to !e appended stream 6 file pointer

:.ample
:.ample%
Cinclude Dstdio.hE int main( A
char aP$RQ6AT$T,T&T,T(T,T*T,TFT,THT,TGT,TIT,TUT,TaTB; F39: >fs; fs6fopen(=#roSect.t.t=,=w= ; fwrite(a,$,$R,fs ; fclose(fs ; return R;

fsee/(
-hi! function !et! the file po!ition in,icator for the !trea. pointe, to b/ !trea. or /ou can !a/ it !ee0! a !pecifie, place )ithin a file an, .o,if/ it.
1$$231$1$$23456 1$$23$78 1ee0! fro. beginning of file 1ee0! fro. current po!ition 1ee0! fro. en, of file

$xa.ple9 :inclu,e Dstdio.hE int .ain( A KKKKKK F39: > f; KKKKKK f 6 fopen(=myfile.t.t=, =w= ; KKKKKK fputs(=Vello World=, f ; KKKKKK fsee/(f, H, "::WM":+ ; KKKKKK fputs(= 3ndia=, f ; KKKKKK fclose(f ; KKKKKK return R; B

"::WMCX), "::WM:O7

ftell(
offset 6 ftell( file pointer ; =ftell= returns the current position for input or output on the file Cinclude Dstdio.hE int main(void A F39: >stream; stream 6 fopen(=08F39:.+;+=, =w= ; fprintf(stream, =+his is a test= ; printf(=+he file pointer is at !yte ?ld<n=, ftell(stream ; fclose(stream ; return R; B

rewind
rewind(fp % resets the file pointer to the !eginning.

+VAOW 8'X

Vous aimerez peut-être aussi