Understanding Working of Text & Binary File in C
Text file
// assume that you have already created a program file first.c and you want to display its contents on screen
Ex 1: Program to show contents of any TEXT File on screen
#include <stdio.h>
main()
{
FILE *fp;
char ch;
clrscr();
fp=fopen(“first.c”,”r”);
if (fp==NULL)
{
printf("Cannot open file");
getch();
return;
}
while((ch=getc(fp))!=eof)
{
printf(“%c”,ch);
getch(); }
fclose(fp);
printf(“\nPress any key to return”);
getch();}
Ex 2: Program to create a copy of first.c
#include <stdio.h>
main()
{
FILE *fp,*fp1;
char ch;
clrscr();
fp=fopen(“first.c”,”r”);
fp1=fopen(“cfirst.c”,”w”);
if (fp==NULL)
{
printf("cannot open file");
getch();
return;
}
if (fp1==NULL)
{
printf("cannot create file”);
getch();
return;
}
while((ch=getc(fp))!=eof)
{
putc(ch,fp1);
getch(); }
fclose(fp);
fclose(fp1);
}
Ex 3: Program to insert data as records into text file
#include <stdio.h>
struct student
{
int rollno;
char sname[20];
int per;
};
main()
{
FILE *fp;
struct student s;
fp=fopen(“student.txt”,”w”);
if (fp==NULL)
{
printf("Error creating file");
getch();
return;
}
while(1)
{
printf(‘\nEnter Roll no(0 to exit)”);
scanf(“%d”,&s.rollno);
if (s.rollno==0)
break;
printf(“Enter Name:”);
fflush(stdin);
gets(s.sname);
printf(“Enter Percentage:”);
scanf(“%d”,&s.per);
fprintf(fp,”%d %s %d”,s.rollno,s.sname,s.per);
}
fclose(fp);
printf(“\n Press enter to return”);
getch();
}
Ex 4: Program to display data as records from text file
#include <stdio.h>
struct student
{
int rollno;
char sname[20];
int per;
};
main()
{
FILE *fp;
int n;
struct student s;
fp=fopen(“student.txt”,”r”);
if (fp==NULL)
{
printf("Error opening/creating file”);
getch();
return;
}
while((fscanf(fp,”%d %s %d”,&s.rollno,s.sname,s.per))!=eof)
{
n=n+1;
printf(“ \n## Record no : %d ##”,n);
printf(“\n Student’s Name is : %s”,s.sname);
printf(“\n Student’s Roll no is %d”,s.rollno);
printf(“\n Student’s Percentage %d”,s.per);
}
printf(“Press enter to return”);
getch();
}
Ex 5: Program to write data as record into binary file
#include <stdio.h>
struct student
{
int rollno;
char sname[20];
int per;
};
main()
{
FILE *fp;
struct student s;
fp=fopen(“student.txt”,”wb”);
if (fp==NULL)
{
printf("Error creating file”);
getch();
return;
}
while(1)
{
printf(‘\nEnter roll no(0 to exit)”);
scanf(“%d”,&s.rollno);
if (s.rollno==0)
break;
printf(“Enter name:”);
fflush(stdin);
gets(s.sname);
printf(“Enter Percentage:”);
scanf(“%d”,&s.per);
fwrite(&s,sizeof(s),1,fp);
}
printf(“\nPress enter to return”);
getch();
}
Ex 6: Program to read data from a binary file
#include <stdio.h>
struct student
{
int rollno;
char sname[20];
int per;
};
main()
{
FILE *fp;
struct student s;
fp=fopen(“student.txt”,”wb”);
if (fp==NULL)
{
printf("Error creating file");
getch();
return;
}
while(( fread(&s,sizeof(s),1,fp))==1)
{
printf(“\n Name of student is %s”,s.sname);
printf(“\n Roll number of student is %d”,s.rollno);
printf(“\n Percentage of student is %d”,s.per);
}
printf(“Press enter to exit”);
getch();
}
If you find any problem in understanding any of the above codes, you can mail your queries at softech.ratlam@gmail.com or just leave a comment in comment box
Bye.............
Comments
Post a Comment