File Handling: Steps To Work On File
File Handling: Steps To Work On File
File Handling: Steps To Work On File
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
FILE *f1,*f2;
• Opening of file:
w-write only(overwrite)
a- append data
2
FILE HANDLING
{
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.
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)
5
exit(0);
}
while((ch=getc(f1))!=EOF)
{
putchar(ch);
}
fclose(f1);
getch();
}
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
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