FILE HANDLING Practical PDF

Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

FILE HANDLING

PRACTICAL ASSIGNMENT
By Sunishtha Singh

Program 1
Write a program that accepts one line of text from
keyboard one character at a time and writes it into
a disk file.
#include<stdio.h>
#include<conio.h>
main()
{
FILE*fp;
char ch;
fp=fopen("test.dat","W");
if(fp==NULL)
{
printf("\nCannot open file");
exit(1);
}
printf("\nType a line of text and press enter key\n");
while((ch==getche())!='r')
fputc(ch,fp);
fclose(fp);
}

Program 2
Write a program to read the same file test1.dat one
character at a time and print the result on screen.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
FILE*fp;
char ch;
if((fp=fopen("test.dat","r"))==NULL)
{
printf("\nCannot open file");
exit(1);
}
while((ch=fgetc(fp))!=EOF)
putchar(ch);
fclose(fp);
getch();
}

Program 3
Write a program to enter strings of data from
keyboard and writes them onto a disk file.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
main()
{
FILE*fp;
char s[80];
if((fp=fopen("test2.dat","w"))==NULL)
{
printf("Cannot opem file");
exit(1);
}
printf("Enter strings of data and press enter key at the
end\n");
while((strlen(gets(s)))>0)
{
fputs(s,fp);
fputs("\n",fp);
}
fclose(fp);
}

Program 4
Write a program to read data from same file
test2.dat one string at a time and writes it onto the
screen.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
main()
{
FILE*fp;
char s[80];
if((fp=fopen("test2.dat","r"))==NULL)
{
printf("Cannot opem file");
exit(1);
}
while(fgets(s,80,fp)!=NULL)
puts(s);
fclose(fp);
getch();
}

Program 5
Write a program to write integer data into a file.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
main()
{
FILE*fp;
char ch='y';
int n;
if((fp=fopen("test3.dat","w"))==NULL)
{
printf("Cannot open file");
exit(1);
}
while(ch=='y')
{
printf("Enter any integer : ");
scanf("%d",&n);
putw(n,fp);
printf("\nWant to enter another number?(Y/N)\n");
ch=getch();
}
fclose(fp);
}

Program 6
Program to read same data from file.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
main()
{
FILE*fp;
int n;
if((fp=fopen("test3.dat","r"))==NULL)
{
printf("Cannot open file");
exit(1);
}
while((n=getw(fp))!=EOF)
printf("%d\n",n);
fclose(fp);
getch();
}

Program 7
Write a program to enter data of mixed types in a
file.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
main()
{
FILE*fp;
char ch='y',name[20];
int age;
float salary;
if((fp=fopen("test4.dat","w"))==NULL)
{
printf("Cannot open file");
exit(1);
}
while(ch=='y')
{
printf("Enter name,age and salary\n");
scanf("%s%d%f",name,&age,&salary);
fprintf(fp,"%s\t%d\t%f\n",name,age,salary);
printf("\nMore records?(Y/N)\n");
ch=tolower(getche());
}
fclose(fp);
}
Program 8
Program to read formatted data from the same file.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
main()
{
FILE*fp;
char name[20];
int age;
float salary;
if((fp=fopen("test4.dat","r"))==NULL)
{
printf("Cannot open file");
exit(1);
}
while(fscanf(fp,"%s%d%f",name,&age,&salary)!=EOF)
printf("%s\t%d\t%f\n",name,age,salary);
fclose(fp);
getch();
}
Program 9
Write a program to write an array of 5 elements to a
file.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<stddef.h>
main()
{
FILE*fp;
int a[5],i;
if((fp=fopen("test5.dat","w"))==NULL)
{
printf("Cannot open file");
exit(1);
}
printf("Enter five numbers,each on one line\n");
for(i=0;i<5;i++)
scanf("%d",&a[i]);
fwrite(a,sizeof(a),1,fp);
fclose(fp);
}

Program 10
Write a program to write a structure to a file.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
main()
{
struct
{
char name[20];
int age;
float sal;
}
emp;
FILE*fp;
char ch='y';
if((fp=fopen("test6.dat","wb"))==NULL)
{
printf("Cannot open file");
exit(1);
}
while(ch=='y')
{
printf("Enter name,age and salary\n");
scanf("%s%d%f",emp.name,&emp.age,&emp.sal);
fwrite(&emp,sizeof(emp),1,fp);
printf("\nMore records?(Y/N)\n");
ch=tolower(getche());
}
fclose(fp);
}
Program 11
Write a program to read structures from a file.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<stddef.h>
main()
{
struct
{
char name[20];
int age;
float sal;
}
emp;
FILE*fp;
if((fp=fopen("test6.dat","rb"))==NULL)
{
printf("Cannot open file");
exit(1);
}
while(fread(&emp,sizeof(emp),1,fp)==1)
printf("\n%s\t%d\t%f\n",emp.name,emp.age,emp.sal);
fclose(fp);
getch();
}

Program 12
Write a program to read a file and count how many
characters, spaces, tabs and newlines are present
in that file.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
main()
{
FILE*fp;
char ch;
int c=0,s=0,t=0,l=0;
if((fp=fopen("file1.dat","wb"))==NULL)
{
printf("Cannot open file");
exit(1);
}
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
c++;
if(ch==' ')
s++;
if(ch=='\t')
t++;
if(ch='\n')
l++;
}
fclose(fp);
printf("Number of characters=%d",c);
printf("\nNumber of blank spaces =%d",s);
printf("\nNumber of tabs=%d",t);
printf("\nNumber of lines=%d",l);
getch();
}

Program 13
Write a program that reads a text file and copies it
into another text file character by character.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
main()
{
FILE*fp,*ft;
char ch;
if((fp=fopen("file1.dat","r"))==NULL)
{
printf("Cannot open source file");
exit(1);
}
if((ft=fopen("file2.dat","w"))==NULL)
{
printf("Cannot open target file");
exit(1);
}
while(1)
{
if((ch==fgetc(fp))!=EOF)
fputc(ch,ft);
else
break;
}
fclose(fp);
fclose(ft);
}
Program 14
Write a program to read data randomly from a file.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<stddef.h>
main()
{
struct
{
char name[20];
int age;
float sal;
}
emp;
FILE*fp;
char ch='y';
long offset;
int n=0;
if((fp=fopen("test7.dat","wb"))==NULL)
{
printf("Cannot open file");
exit(1);
}
printf("Enter number of employees\n");
while(ch=='y')
{
printf("\nRecord no.%d",n);
printf("\nEnter name:");
gets(emp.name);
printf("Enter age:");
scanf("%d",&emp.age);
printf("Enter salary:");
scanf("%f",&emp.sal);
if(fwrite(&emp,sizeof(emp),1,fp)<=0)
{
printf("\nDisk error of write");
exit(1);
}
printf("\nMore records?(Y/N)\n");
ch=getche();
fflush(stdin);
n++;
}
fclose(fp);
if((fp=fopen("test7.dat","rb"))==NULL)
{
printf("Cannot open file");
exit(1);
}
ch='y';
while(ch=='y')
{
printf("\nEnter the records to access:");
scanf("%d",&n);
offset=(long)n*sizeof(emp);
fseek(fp,offset,0);
if(fread(&emp,sizeof(emp),1,fp)==0)
{
printf("\nDisk error of read");
exit(1);
}
printf("\nTh employee data is\n");
printf("\nName=%s",emp.name);
printf("\nAge=%d",emp.age);
printf("\nSalary=%f",emp.sal);
printf("\nMore records?(Y/N)");
ch=getche();
fflush(stdin);
rewind(fp);
}
fclose(fp);
}

Program 15
Demonstration off command line programs.
#include<stdio.h>
#include<conio.h>
main(int argc,char*argv[])
{
int i;
printf("Command =%s",argv[0]);
printf("\nNo. of arguments=%d",argv);
printf("\nArguments are \n");
for(i=0;i<argc;i++)
printf("\n argv[%d]=%s",i,argv[i]);
}

Program 16
Write a program to implement copy command of
DOS using command line arguments.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<stddef.h>
#include<ctype.h>
main(int argc,char*argv[])
{
FILE*fs,*ft;
char ch;
if(argc!=3)
{
printf("No. of parameteres are Wrong");
exit(1);
}
fs=fopen(argv[1],"r");
if(fs==NULL)
{
puts("Cannot open source file");
exit(1);
}
ft=fopen(argv[2],"w");
if(ft==NULL)
{
puts("\Cannot open target file");
fclose(fs);
exit(1);
}
while((ch=fgetc(fs))!=EOF)
fputc(ch,ft);
fclose(fs);
fclose(ft);
}

You might also like