1. File Management in C (Topic 7: Files GTU Syllabus 2009)
Chaudhari Technical Institute MCA (506) GandhinagarCreated By:
Vinod
[email protected]://vinodthebest.wordpress.com
2. File Management We have been using the functions such as
printf and scanf to read and write data. This works fine as long as
data is small. But it has two major problems: It becomes cumbersome
and time consuming to handle large volume of data through
terminals. The entire data is lost when either the program is
terminated or the computer is turned off. Prepared By: Vinod Pillai
Chaudhari Technical Institute - Gandhinagar 2
3. File Management So it is necessary to have more flexible
approach where data can be stored on the disks and read whenever
necessary, without destroying data. This concept is called files.
Files: A file is a place on the disk where a group of related data
is stored. FILE is a structure declared in stdio.h . We have to use
file pointer, a pointer variable that points to a structure FILE.
Prepared By: Vinod Pillai Chaudhari Technical Institute -
Gandhinagar 3
4. C File Management C supports a number o functions that have
the ability to perform basic file operations: Creating a file
Opening a file Reading data from a file Writing data to a file
Closing a file Renaming a file Deleting a file Prepared By: Vinod
Pillai Chaudhari Technical Institute - Gandhinagar 4
5. Using Files in CDeclaration of File Pointer Checking the
result of fopen()FILE *fp;FILE *fp1; if(fp == NULL) {Opening a File
printf(Cant open); exit(1);fp=fopen(one.txt,r); } Prepared By:
Vinod Pillai Chaudhari Technical Institute - Gandhinagar 5
6. Using Files in CClosing Files fclose(fp); Prepared By: Vinod
Pillai Chaudhari Technical Institute - Gandhinagar 6
7. File Opening Modesr= Open file for Reading r+ = Open file
forw= Create file for Writing read/writea= Append text file w+ =
Create file for read/write a+ = Append text file for read/writerb =
Open binary file for r+b = Open binary file for Reading
read/writewb= Create binary file w+b= Create binary file for for
Writing read/writeab = Append binary file a+b= Append binary file
for read/write Prepared By: Vinod Pillai Chaudhari Technical
Institute - Gandhinagar 7
8. Working with Text filesFour functions for read Four
functions for writefiles from the disk: files into the
disk:fscanf() fprintf()fgets() fputs()fgetc() fputc()fread()
fwrite() Prepared By: Vinod Pillai Chaudhari Technical Institute -
Gandhinagar 8
9. Write Character to a file (Pg. 583)Char name[30];
While(name*i+!=0)int i=0; { putc(name[i],fp);FILE *fp;
i++;fp=fopen(one.txt,w); }Printf(Enter Name:);
fclose(fp);Scanf(%*^n+,name); Prepared By: Vinod Pillai Chaudhari
Technical Institute - Gandhinagar 9
10. Reading Character from file (Pg. 584)int ch; //IMP
While(ch!=EOF) {FILE *fp; putchar(ch); //IMPfp=fopen(one.txt,r);
ch=getc(fp); }ch=getc(fp); fclose(fp); Prepared By: Vinod Pillai
Chaudhari Technical Institute - Gandhinagar 10
11. Reading Character from file (Pg. 584)int ch; //IMP
While(ch!=EOF) {FILE *fp; putchar(ch); //IMPfp=fopen(one.txt,r);
ch=getc(fp); }ch=getc(fp); fclose(fp); Prepared By: Vinod Pillai
Chaudhari Technical Institute - Gandhinagar 11
12. ProgramsExample 4 : Count the int ch1,ch2; number of
characters FILE *fp1,*fp2; and number of lines. (Pg. 586)
fp1=fopen(one.txt,r); fp2=fopen(two.txt,r);Example 6: Compare two
files. (Pg. 588) ch1=getc(fp1); ch2=getc(fp2); Prepared By: Vinod
Pillai Chaudhari Technical Institute - Gandhinagar 12