File – a place on disc where group of related data is stored.
E.g. your C programs, executable, word file, excel file etc.
High-level programming languages support file operations.
Operations on file.
- Naming
- Opening
- Reading
- Writing
- Closing
When to use File Handling?
- Large data volumes
- E.g. physical experiments, human genome, population records etc.
- Need for store/retrieve data
Defining and opening file:-
To store data file in secondary memory (disc) must specify to OS.
- File name (e.g. fun.c, result.data).
- Data structure (e.g. FILE).
- Purpose (e.g. reading, writing, appending).
Different modes:-
- Writing mode(w) :- if file already exists then contents are deleted, else new file with specified name created.
- Appending mode(a):- if file already exists then file opened with contents safe else new file created.
- Reading mode(r):- if file already exists then opened with contents safe else error occurs.
Input/Output operations on files:
C provides several different functions for reading/writing.
- getc() – read a character
- putc() – write a character
- fprintf() – write set of data values
- fscanf() – read set of data values
- getw() – read integer
- putw() – write integer
Errors that occur during I/O
Typical errors that occur
- trying to read beyond end-of-file.
- trying to use a file that has not been opened.
- perform operation on file not permitted by ‘fopen’ mode.
- open file with invalid filename.
- write to write-protected file.
C Program to demonstrate File handling program
#include<conio.h> #include<stdio.h> void main() { char c ; FILE *fptr1 ; clrscr() ; printf("Enter the text to be stored in the file.\n") ; printf("Use ^Z or F6 at the end of the text and pressENTER: \n\n") ; fptr1 = fopen("myfile.DAT","w") ; while((c = getc(stdin)) != EOF) fputc(c, fptr1) ; fclose(fptr1) ; printf("\nThe content of the file is : \n\n") ; fptr1 = fopen("myfile.DAT", "r") ; do { c = fgetc(fptr1) ; putchar(c) ; } while(c != EOF) ; fclose(fptr1) ; getch() ; }
/* opening the contents of the file */ #include <stdio.h> #include <conio.h> void main() { FILE *fp; int c ; fp = fopen("file2.c", "r"); c = getc( fp ) ; while ( c != EOF ) { putchar(c); c=getc (fp); } fclose(fp) ; }
/* open the file based on user input */ #include <stdio.h> #include <conio.h> void main() { FILE *fopen(), *fp; int c ; char filename[40] ; clrscr(); printf("Enter file to be displayed:"); gets(filename) ; fp=fopen(filename,"r"); c=getc(fp) ; while(c!=EOF ) { putchar(c); c=getc(fp ); } fclose(fp); getch(); }
/* Count no. of characters in a file*/ #include <stdio.h> #include <conio.h> void main() /* Prompt user for file and count number of characters and lines in it*/ { FILE *fp; int c , nc, nlines; char filename[40] ; clrscr(); nlines=0 ; nc=0; printf("Enter file name: "); gets( filename ); fp=fopen( filename, "r" ); if(fp == NULL) { printf("Cannot open %s for reading \n", filename ); exit(0); /* terminate program */ } c = getc( fp ) ; while ( c != EOF ) { if ( c == '\n' ) nlines++ ; nc++ ; c = getc ( fp ); } fclose( fp ); if ( nc != 0 ) { printf("There are %d characters in %s \n", nc, filename ); printf("There are %d lines \n", nlines ); } else printf("File: %s is empty \n", filename ); getch(); }
/* compare two files */ #include <conio.h> #include <stdio.h> void main() { FILE *fp1, *fp2; int ca, cb; char fname1[40], fname2[40] ; printf("Enter first filename:") ; gets(fname1); printf("Enter second filename:"); gets(fname2); fp1 = fopen( fname1, "r" ); /* open for reading */ fp2 = fopen( fname2, "r" ) ; /* open for reading */ if ( fp1 == NULL ) /* check does file exist etc */ { printf("Cannot open %s for reading \n", fname1 ); exit(0); /* terminate program */ } else if ( fp2 == NULL ) { printf("Cannot open %s for reading \n", fname2 ); exit(0); /* terminate program */ } else /* both files opened successfully */ { ca = getc( fp1 ) ; cb = getc( fp2 ) ; while ( ca != EOF && cb != EOF && ca == cb ) { ca = getc( fp1 ) ; cb = getc( fp2 ) ; } if ( ca == cb ) printf("Files are identical \n"); else if ( ca != cb ) printf("Files differ \n" ); fclose ( fp1 ); fclose ( fp2 ); } getch(); }
/* Copy file3.c to file3.txt */ #include <conio.h> #include <stdio.h> void main() { FILE *fp1, *fp2; int c ; fp1 = fopen( "file3.c", "r" ); fp2 = fopen( "file3.txt", "w" ); if ( fp1 == NULL ) { printf("Cannot open file3.c for reading \n" ); exit(1); } else if ( fp2 == NULL ) { printf("Cannot open file3.txt for writing \n"); exit(1); } else { c = getc(fp1) ; while ( c != EOF) { putc( c, fp2); c = getc( fp1 ) ; } fclose ( fp1 ); fclose ( fp2 ); printf("Files successfully copied \n"); } getch(); }
/*Program using fprintf & fscanf functions in file Handling */ #include<stdio.h> #include<conio.h> main() { FILE *fp; int num,qty,I; float price,value; char item[10],filename[10]; printf(“Enter filename”); scanf(“%s”,filename); fp=fopen(filename,”w”); printf(“Input Some data\n\n”0; printf(“Enter Values for Item name number price quantity\n”); for (I=1;I< =3;I++) { fscanf(stdin,”%s%d%f%d”,item,&number,&price,&quality); fprintf(fp,”%s%d%f%d”,itemnumber,price,quality); } fclose (fp); fprintf(stdout,”\n\n”); fp=fopen(filename,”r”); /*Reading data from file*/ printf(“Item name number price quantity value”); for(I=1;I< =3;I++) { fscanf(fp,”%s%d%f%d”,item,&number,&prince,&quality); value=price*quantity”); fprintf(“stdout,”%s%d%f%d%d\n”,item,number,price,quantity,value); } fclose(fp); }
/*C Program to handle mixed data types*/ #include<stdio.h> #include<conio.h> main() { FILE *fp; int num,qty,I; float price,value; char item[10],filename[10]; printf("Input filename"); scanf("%s", filename); fp=fopen(filename,"w"); printf("Input inventory datann"); printf("Item name, number, price, quantity\n"); for I=1;I< =3;I++) { fscanf(stdin,"%s%d%f%d",item,&number,&price,&quality); fprintf(fp,"%s%d%f%d",itemnumber,price,quality); } fclose (fp); fprintf(stdout,"nn"); fp=fopen(filename,"r"); printf("Item name number price quantity value"); for(I=1;I< =3;I++) { fscanf(fp,"%s%d%f%d",item,&number,&prince,&quality); value=price*quantity”); fprintf(stdout,"%s%d%f%d%dn",item,number,price,quantity,value); } fclose(fp); }