Thursday, July 21, 2011

Program to copy two files into single another file

/*Serial No.109     [swami86.cpp]*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
int i;
char ch,name[10];
clrscr();
FILE *fp1,*fp2,*fp3;
fp1=fopen("swami1.dat","r");
if(fp1==NULL)
    {
    printf("file doesn't exist");
    getch();
    fclose(fp1);
    exit;
    }
fp3=fopen("swami3.dat","w");
if(fp3==NULL)
    {
    printf("file doesn't exist");
    getch();
    fclose(fp3);
    exit;
    }
printf("file is being copied.\n");
while(1)
    {
    ch=fgetc(fp1);
    if(ch==EOF)
    break;
    else
    fputc(ch,fp3);
    }
fclose(fp1);
fclose(fp3);
fp2=fopen("swami2.dat","r");
if(fp2==NULL)
    {
    printf("file doesn't exist");
    getch();
    fclose(fp2);
    exit;
    }
fp3=fopen("swami3.dat","a+");
if(fp3==NULL)
    {
    printf("file doesn't exist");
    getch();
    fclose(fp3);
    exit;
    }
printf("second file is being copied.\n");
while(1)
    {
    ch=fgetc(fp2);
    if(ch==EOF)
    break;
    else
    fputc(ch,fp3);
    }
fclose(fp2);
fclose(fp3);
getch();
}

No comments:

Post a Comment

If you have any doubt, feel free to ask...