Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 12

FILE HANDLING

File handling in C enables us to create, update, read, and delete the files stored on the lo
cal file system through our C program. The following operations can be performed on a fil
e.
Creation of the new file
Opening an existing file
Reading from the file
Writing to the file
Deleting the file
Steps to work on file
1.Declaration of file pointer.
2.Opening of file.
3.Operation on file.
4.Closing on file.

1
FILE HANDLING

• Declaration of file pointer.


Like int*j;

FILE *f1,*f2;
• Opening of file:

File pointer= fopen(“filename”, “mode”);


Mode:
r- read only

w-write only(overwrite)
a- append data

f1= fopen(“abc.txt”, “r”);


f2= fopen(“xyz.txt”, “w”);

2
FILE HANDLING

• To check existence of file:


if(f1==NULL||f2==NULL)

{
printf(“File doesn’t exist”);

exit(0);
}
• Operation on file:
char ch;
ch=getchar(); It receives single character from user(keyboard)

ch=getc(file pointer)
ch=getc(f1);It takes single character from file.

putchar(ch); Print Character on monitor.


putc(ch,f2); Print character on file.
3
FILE HANDLING

For integer To check existence of file


int i; if(f1==NULL||f2==NULL)
getw(file pointer ->getw(f1) {
putw(variable,file pointer) ->putw(i,f2); printf(“file doesn’t exist”);
For console exit(0);
scanf(“%d”,&i); }
printf(“%d”,i); Closing of file
For file fclose(f1);
fscanf(f1, “%d”,&i); fclose(f2);
fprintf(f2, “%d”,i);

4
PROGRAM TO READ CONTENT FROM FILE ABC.TEXT AND PRIN
T IT ON SCREEN

#include<process.h>
#include<stdio.h>

#include<conio.h>
void main()

{ char ch;
FILE *f1;
clrscr();

f1=fopen("abc.text","r");
if(f1==NULL)

{ printf("file does not exist");

5
exit(0);
}

while((ch=getc(f1))!=EOF)
{

putchar(ch);
}

fclose(f1);
getch();
}

Note: You can use <stdlib.h> also in place of <process.h>

6
PROGRAM TO READ CHARACTER FROM KEYBOARD AND WRIT
E IT ON FILE XYZ.TEXT

#include<process.h> }
#include<stdio.h> while((ch=getchar())!=EOF)
#include<conio.h> {
void main() putchar(ch,f2);
{ char ch; }
FILE *f2; fclose(f2);
clrscr(); getch();
f2=fopen("xyz.text","w"); }
if(f2==NULL)
{ printf("file does not exist");
exit(0);

7
EXPLAIN FEW FILE HANDLING LIBRARY FUNCTION WITH SYNTA
X

Function Description Syntax


filepointer=fopen("file","mod
fopen() Used to open the file
e")
fclose() used to close the file fclose(filepointer)
It read single character from
getc() getc(filepointer)
file
It writes a single character
putc() putc(variable, filepointer)
to file
It read single integer from
getw() getw(filepointer)
file
putw() It write single integer to file putw(variable,filepointer)
It gives current location of
ftell() ftell(filepointer)
pointer
It set pointer to desired
fseek() fseek(filepointer,n,refpos)
location

8
PROGRAM TO COPY THE CONTENT OF FILE SAY ABC.TEXT TO X
YZ.TEXT

#include<process.h> }
#include<stdio.h> while((ch=getc(f1))!=EOF)
#include<conio.h> {
void main() putc(ch,f2);
{ char ch; }
FILE *f1, *f2; fclose(f1);
clrscr(); fclose(f2)
f1=fopen("abc.text","r"); getch();
f2=fopen("xyz.text","a"); }
if(f1==NULL||f2==NULL)
{ printf("file does not exist");
exit(0);
9
PROGRAM TO READ INTEGER VALUE FROM ONE FILE & WRITE
SQUARE OF EACH IN ANOTHER FILE

#include<process.h> exit(0); }
#include<stdio.h> while((ch=getw(f1))!=EOF)
#include<conio.h> { ch=ch*ch;
void main() putw(ch,f2);
{ int ch; }
FILE *f1, *f2; fclose(f1);
clrscr(); fclose(f2);
f1=fopen("abc.text","r"); getch(); }
f2=fopen("xyz.text","a");
if(f1==NULL||f2==NULL)
{ printf("file does not exist");

10
PROGRAM TO READ DATA FROM FILE AND CHECK WHETHER E
VEN OR ODD, IF EVEN WRITE IN FILE EVEN.TEXT OTHERWISE O
DD.TEXT

#include<process.h> while((ch=getw(f3))!=EOF)
#include<stdio.h> {
#include<conio.h> if(ch%2==0)
void main() putw(ch,f1);
{ int ch; else
FILE *f1,*f2,*f3; putw(ch,f2);
clrscr(); }
f1=fopen("even.text","a"); fclose(f1);
f2=fopen("odd.text","a"); fclose(f2);
f3=fopen("input.text","r") fclose(f3)
if(f1==NULL||f2==NULL|f3==NULL|) getch();
{ printf("file does not exist"); }
exit(0); 11
12

You might also like