Write a Program to insert the following contents in a file
named “File1”.
Customer No. Account Type Balance
101 Savings 2000
102 Current 5000
103 Savings 3000
104 Current 10000
Append the contents of “File1” in another file “File2”. Also
display the contents of File2 on screen.
CODING:
#include <stdio.h>
#include<conio.h>
void main()
{
FILE *fp1,*fp2;
struct file{
int cno,bal;
char actype[8];
}cdetails[4]={
{101,2000,"Savings"},
{102,5000,"Current"},
{103,3000,"Savings"},
{104,10000,"Current"}
};
char ch;
int a;
clrscr();
fp1=fopen("file1.txt","w");
fp2=fopen("file2.txt","a+");
if((fp1||fp2)==NULL)
{
printf("\nError in handling file(s).");
getch();
exit();
}
fprintf(fp1,"Customer No.\tAccount Type\tBalance\n");
fprintf(fp2,"Customer No.\tAccount Type\tBalance\n");
for(a=0;a<4;a++)
{
fprintf(fp1,"%d\t\t",cdetails[a].cno);
fprintf(fp1,"%s\t\t",cdetails[a].actype);
fprintf(fp1,"%d\n",cdetails[a].bal);
fprintf(fp2,"%d\t\t",cdetails[a].cno);
fprintf(fp2,"%s\t\t",cdetails[a].actype);
fprintf(fp2,"%d\n",cdetails[a].bal);
}
printf("\nData copied to file1.txt successfully.");
printf("\nContents of file1.txt appended to file2.txt successfully.");
getch();
clrscr();
printf("\nContents of file2.txt :\n\n");
rewind(fp2);
while(!feof(fp2))
{
ch=fgetc(fp2);
printf("%c",ch);
}
fcloseall();
getch();
}
named “File1”.
Customer No. Account Type Balance
101 Savings 2000
102 Current 5000
103 Savings 3000
104 Current 10000
Append the contents of “File1” in another file “File2”. Also
display the contents of File2 on screen.
CODING:
#include <stdio.h>
#include<conio.h>
void main()
{
FILE *fp1,*fp2;
struct file{
int cno,bal;
char actype[8];
}cdetails[4]={
{101,2000,"Savings"},
{102,5000,"Current"},
{103,3000,"Savings"},
{104,10000,"Current"}
};
char ch;
int a;
clrscr();
fp1=fopen("file1.txt","w");
fp2=fopen("file2.txt","a+");
if((fp1||fp2)==NULL)
{
printf("\nError in handling file(s).");
getch();
exit();
}
fprintf(fp1,"Customer No.\tAccount Type\tBalance\n");
fprintf(fp2,"Customer No.\tAccount Type\tBalance\n");
for(a=0;a<4;a++)
{
fprintf(fp1,"%d\t\t",cdetails[a].cno);
fprintf(fp1,"%s\t\t",cdetails[a].actype);
fprintf(fp1,"%d\n",cdetails[a].bal);
fprintf(fp2,"%d\t\t",cdetails[a].cno);
fprintf(fp2,"%s\t\t",cdetails[a].actype);
fprintf(fp2,"%d\n",cdetails[a].bal);
}
printf("\nData copied to file1.txt successfully.");
printf("\nContents of file1.txt appended to file2.txt successfully.");
getch();
clrscr();
printf("\nContents of file2.txt :\n\n");
rewind(fp2);
while(!feof(fp2))
{
ch=fgetc(fp2);
printf("%c",ch);
}
fcloseall();
getch();
}
Comments
Post a Comment