C Programming - Files Handling
C Programming - Files Handling
Data organization
All data stored on the disk in a binary from this binary data is stored on the disk
varius form on operating system (OS) to another. It have to use any library function
written for the particular OS to be able to perform input/Output
File Operation
There are different operations that can be carried out on a file
When working with a stream oriented data file, the first step is to establish a
buffer area where information is temporarily stored while being transferred between the
computer memory and the data file
FILE *fp;
Where FILE (upper case letter) is a special structure the buffer area and fp is
pointer variable that indicate the beginning of the buffer area.
I/O Function
fopen() Create a new file for use open an existing file for use.
fclose() Close a file which has been opened for use
File type/Mode
void main()
{
FILE *fp;
char name[20];
clrscr();
printf(“\n Enter the file that you want to search”);
gets(name);
fp=fopen("name","w");
if(fp==NULL)
printf("\n File is not found ");
else
printf("\n File is available ");
fclose(fp);
getch();
}
putc(c,fp);
Getc is used to read a character from that has been opened on read mode “r”
c=getc(fp)
2. Write a C program accept any name again write name in the particular file then display
on the screen from the file using getc() and putc()
void main()
{
FILE *fp;
char c;
clrscr();
fp=fopen("abc.txt","w");
printf("\n Enter any name ");
while((c=getchar())!='\n')
{
putc(c,fp);
}
fclose(fp);
fp=fopen("abc.txt","r");
while((c=getc(fp))!=EOF)
{
putchar(c);
}
fclose(fp);
getch();
}
3. Write a C program accept any name again write name in the particular file then display
on the screen from the file using fprintf() and fscanf()
void main()
{
FILE *fp;
int i=0;
char name[20],str[20];
clrscr();
fp=fopen("abc.txt","w");
printf("\n Enter any name ");
while((name[i++]=getchar())!='\n');
fprintf(fp,"%s",name);
fclose(fp);
fp=fopen("abc.txt","r");
fscanf(fp,"%s",str);
printf("\n%s",str);
fclose(fp);
getch();
}
4. Write a C program find out the area of circle of given radius of circle using file handling
#define PI 3.14
void main()
{
FILE *fp;
int r;
float a;
fp=fopen("abc.txt","w");
clrscr();
printf("\n Enter the Radius =?");
scanf("%d",&r);
putw(r,fp);
fclose(fp);
fp=fopen("abc.txt","r");
r=getw(fp);
a=PI*r*r;
printf("\n Area of clrcle %f",a);
fclose(fp);
getch();
}
5. Create a file “number.txt” and strore list of 100 integers in it. Using the file
“number.dat”, write a C program which create two more to store even and odd integers
separately.
6. Write a Program to read 1 to 100 and write even number in a file(even.dat) and odd
number(odd.dat)
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp,*fp1,*fp2;
int n,i,m;
clrscr();
fp=fopen("number.txt","w");
printf("\n Enter 10 number ");
for(i=0;i<10;i++)
{
printf("\n Enter %d number",i+1);
scanf("%d",&n);
putw(n,fp);
}
fclose(fp);
fp=fopen("number.txt","r");
printf("\n Odd Number :");
while((n=getw(fp))!=EOF)
{
printf(" %d",n);
}
fclose(fp);
fp=fopen("number.txt","r");
fp1=fopen("odd.txt","w");
fp2=fopen("even.txt","w");
while((n=getw(fp))!=EOF)
{
if(n%2==0)
putw(n,fp2);
else
putw(n,fp1);
}
fclose(fp);
fclose(fp1);
fclose(fp2);
fp=fopen("even.txt","r");
printf("\n Even Number :");
while((n=getw(fp))!=EOF)
{
printf(" %d",n);
}
fclose(fp);
fp=fopen("odd.txt","r");
printf("\n Odd Number :");
while((n=getw(fp))!=EOF)
{
printf(" %d",n);
}
fclose(fp);
getch();
}
7. Write a program in C that takes ten integers from a file and write square of these integer
into another file
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *fp, *fp1;
int n,s;
clrscr();
fp=fopen("abc.txt","w");
printf("\n Tyep 0 when finished ");
printf("\n Enter any number ");
scanf("%d",&n);
while(n!=0)
{
putw(n,fp);
printf("\n Enter any number ");
scanf("%d",&n);
}
fclose(fp);
fp=fopen("abc.txt","r");
fp1=fopen("qaz.txt","w");
while((n=getw(fp))!=EOF)
{
s=n*n;
putw(s,fp1);
}
fclose(fp);
fclose(fp1);
fp=fopen("qaz.txt","r");
while((n=getw(fp))!=EOF)
{
printf("\n%d",n);
}
fclose(fp);
getch();
}
8. Write a program in C to copy the content of given file say “a.txt” to another file “b.txt”.
void main()
{
FILE *fp,*fp1;
char c; // Open a.txt file and write the content
fp=fopen("a.txt","w");
clrscr();
printf("\n Enter any string ");
while((c=getchar())!='\n')
putc(c,fp);
fclose(fp); // Copy the content one file to another file
fp=fopen("a.txt","r");
fp1=fopen("b.txt","w");
while((c=getc(fp))!=EOF)
{
putc(c,fp1);
}
fclose(fp);
fclose(fp1); // Dipslat the content file a.txt
fp=fopen("a.txt","r");
while((c=getc(fp))!=EOF)
{ putchar(c);
}
fclose(fp); // Display the content file b.txt
fp=fopen("b.txt","r");
while((c=getc(fp))!=EOF)
{ putchar(c);
}
fclose(fp);
getch();
}
9. Write a program in C to create a text file. Also copy the vowels in that file to another file
10. What are the various types of files that can be created in C language? Also give different
modes in which these files can be used with proper syntax. Write a program in C
language to append some more text at end of an existed text file.
11. List out carious file operation in ‘C’. Writes a C program to count the number of
character in a file.
12. Write a program in C implement file handling with strudture such as create list, display
list, sorting list, searching list, append (new record add) and delete record
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
void create();
void display();
void sorting();
void searching();
void append();
void delrec();
struct student
{
int roll;
char name[15];
};
struct student stu;
void main()
{
int n;
while(1)
{
clrscr();
printf("\n\n 1. CREATE ");
printf("\n\n 2. DISPLAY ");
printf("\n\n 3. SORTING ");
printf("\n\n 4. SEARCHING ");
printf("\n\n 5. APPEND ");
printf("\n\n 6. DELETE RECORD ");
printf("\n\n 0. EXIT ");
printf("\n\n ENTER YOUR OPTION ");
scanf("%d",&n);
switch(n)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
sorting();
break;
case 4:
searching();
getch();
break;
case 5:
append();
break;
case 6:
delrec();
break;
case 0:
exit(1);
}
}
}
void create()
{
FILE *fp;
char n[15],ans='y';
int r;
fp=fopen("asd.txt","w");
while(ans=='y')
{
}
void searching()
{
char str[15];
int n,r,flag,i;
FILE *fp;
fp=fopen("asd.txt","r");
printf("\n 1. FOR ROLL NUMBER ");
printf("\n 2. FOR NAME ");
printf("\n Enter your choice ");
scanf("%d",&n);
if(n==1)
{
printf("\n Enter Roll number to searched ");
scanf("%d",&r);
flag=0;
while(fread(&stu,sizeof(stu),1,fp))
{
if(stu.roll==r)
{
printf("\n %d %s",stu.roll,stu.name);
flag=1;
break;
}
}
if(flag==0)
printf("\n Roll number is not found");
}
else
if(n==2)
{
printf("\n Enter Name to searched ");
scanf("%s",&str);
flag=0;
while(fread(&stu,sizeof(stu),1,fp))
{
if(strcmp(stu.name,str)==0)
{
printf("\n %d %s",stu.roll,stu.name);
flag=1;
break;
}
}
if(flag==0)
printf("\n Name is not found");
}
else
printf("\n Again try ");
}
void append()
{
FILE *fp;
char n[15];
int r;
fp=fopen("asd.txt","a");
printf("\n Enter roll No. ");
scanf("%d",&r);
printf("\n Enter name ");
scanf("%s",n);
stu.roll=r;
strcpy(stu.name,n);
fwrite(&stu,sizeof(stu),1,fp);
fclose(fp);
}
void delrec()
{
int r,flag=0,i;
FILE *fp,*fp1;
fp=fopen("asd.txt","r");
fp1=fopen("qaz.txt","w");
printf("\n ENTER FOR ROLL NUMBER FOR DELETED ");
scanf("%d",&r);
while(fread(&stu,sizeof(stu),1,fp))
{
if(stu.roll==r)
{
flag=1;
}
else
fwrite(&stu,sizeof(stu),1,fp1);
}
fclose(fp);
fclose(fp1);
fp1=fopen("qaz.txt","r");
fp=fopen("asd.txt","w");
while(fread(&stu,sizeof(stu),1,fp1))
fwrite(&stu,sizeof(stu),1,fp);
fclose(fp);
fclose(fp1);
if(flag==0)
{
printf("\n Roll Number Not found");
getch();
}
13. Define a structure to store employee record eg. Employee if, employee name and salary
of employee. Using this structure write a C program to create a file “employee.dat”.
There must be one record for every employeein the file. Accept the data from the user.
Using the file “employee.dat”, display the detail of employee if is entered by the user
14. Write a C program using command line argument display the content, the given file.
File name is ‘shatype.c’
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(int argc, char *argv[])
{
int i;
FILE *fp;
char str[20],c;
i=argc;
if(i==2)
{
strcpy(str,argv[1]);
fp=fopen(str,"r");
if(fp==NULL)
{
printf("\n File is not available ");
}
else
{
while((c=fgetc(fp))!=EOF)
putchar(c);
fclose(fp);
}
}
else
{
printf("\n Invalid parameter ");
getch();
}
}
15. Write a C program using command line argument copy one file to another file
Example shacopy qaz.txt asd.txt
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(int argc, char *argv[])
{
int i=argc;
FILE *fp1,*fp2;
char str1[20],char str2[20],c;
if(i==3)
{
strcpy(str1,argv[1]);
strcpy(str2,argv[2]);
fp1=fopen(str1,"r");
if(fp1==NULL)
printf("\n File is not available ");
else
{
fp2=fopen(str2,"w");
while((c=fgetc(fp1))!=EOF)
fputc(c,fp2);
fclose(fp1);
fclose(fp2);
printf("\n 1 file(s) copied. ");
}
}
else
{
printf("\n Invalid parameter ");
getch();
}
}