21-03-2011, 04:10 PM
12438_Lect 48-49-50-51(2).ppt (Size: 151 KB / Downloads: 51)
FILE HANDLING
• PROBLEM:-if we need same data to be processed again and again,we have to enter it again and again and if volume of data is large,it will be time consuming process.
• SOLUTION:-data can be permanently stored,a program can read it directly at a high speed.
• The permanent storage space on a disk is called FILE.
• FILE is a set of records that can be accessed through the set of library functions.
FILE TYPES
1.SEQUENTIAL FILE:-in this data is kept permanently.if we want to read the last record of file then we need to read all the records before that record.it means if we wan to read 10th record then the first 9 records should be read sequentially for reaching to the 10th record.
2.RANDOM ACCESS FILE:- in this data can be read and modified randomly.if we want to read the last record of the file,we can read it directly.it takes less time as compared as sequential file.
HOW TO DEFINE A FILE
Before using a file in a program,we must open it,which establishes a link between the program and the operating system.A file must be defined as:-
FILE *fp;
data structure file pointer
where FILE is the data structure which is included in the header file,STDIO.H and fp is declared as a pointer which contains the address of a character which is to be read.
Operation on a file
fp = fopen (“ABC.DOC”, “r”);
Here fp =file pointer
fopen= function to open a file
ABC.DOC=name of the file
r=mode in which file is to be opened
fopen is a function which contains two arguments:-
1. File name ABC.DOC
2. Mode in which we want to open the file.
3. Both these are of string types.
fopen():-
• This function loads the file from disk to memory and stores the address of the first character to be read in the pointer variable fp.
• If file is absent,it returns a NULL pointer variable.
fclose(fp):- the file which is opened need to be closed and only can be done by library function fclose.
File opening modes
1. Read mode®:- purpose to read from the file.if file exists,it sets up a pointer which points to first char in it and if it does not exist,it returns NULL.
2. Write mode(w):-purpose to write in the file.if file exists,its contents are overwritten.if it does not exist new file is created.
3. Append mode(a):-purpose is to add data in already existing file.if it does not exist new file is created.
4)Read +(r +):-it is an extension to read mode.the operations possible on this mode are to read existing,writing new contents and modifying existings contents.it returns NULL if file does not exist.
5)Write +(w +):-it is similar to write mode, mode.the operations possible on this mode are to write new contents and modify contents already written.
6)Append +(a +):- it is similar to append mode,the operations possible on this mode are to read existing contents,add new contents at the end of file but cannot modify existing contents.