Write a program in C, to copy file1 to another file2 in the same directory 7m Jun2007

By | July 11, 2014

Write a program in C, to copy file1 to another file2 in the same directory. 7m Jun2007

Code:

#include<stdio.h>
void main()
{
int ch,size=0;
FILE *fp1;
FILE *fp2;
clrscr();
if((fp1=fopen(“readme.txt”,”r”))==NULL)
{
printf(“FILE DOES NOT EXIST\n”);
exit(0);
}
if((fp2=fopen(“duplicate.txt”,”w”))==NULL)
{
printf(“FILE CAN NOT BE CREATED\n”);
exit(0);
}
while(!feof(fp1))
{
ch=getc(fp1);
putc(ch,fp2);
size++;
}
fclose(fp1);
fclose(fp2);
printf(” size in bytes = %d”,size);
getch();
}

#include<stdio.h>
void main()
{
int ch,size=0;
FILE *fp1;
FILE *fp2;
clrscr();
if((fp1=fopen("readme.txt","r"))==NULL)
{
printf("FILE DOES NOT EXIST\n");
exit(0);
}
if((fp2=fopen("duplicate.txt","w"))==NULL)
{
printf("FILE CAN NOT BE CREATED\n");
exit(0);
}
while(!feof(fp1))
{
ch=getc(fp1);
putc(ch,fp2);
size++;
}
fclose(fp1);
fclose(fp2);
printf(" size in bytes = %d",size);
getch();
}

Screen Shots:

C_program_File_Copy

 

C_program_File_Copy_Output

Leave a Reply