Write a C program which copies one file to another.
(Note: The file name and n are specified on the command line.)

Algorithm:

Step 1: Start
Step 2: read command line arguments
Step 3: check if no of arguments = 3 or not. If not print invalid no of arguments
Step 4: open source file in read mode
Step 5: if NULL pointer, then print source file can not be open
Step 6: open destination file in write mode
Step 7: if NULL pointer, then print destination file can not be open
Step 8 : read a character from source file and write to destination file until EOF
Step 9: close source file and destination file
Step 10: Stop

Program:

#include <stdio.h>
#include <conio.h>
void main()
{
   FILE *f1, *f2;
   char c;
   clrscr();
   printf(“Enter the data to the file1.txt file \n”);
   f1 = fopen(“file1.txt”, “w”);
   while((c = getchar()) != EOF)
   putc(c, f1);
   fclose(f1);
   f2 = fopen(“file2.txt”, “w”);
   f1 = fopen(“file1.txt”, “r”);
   while((c = getc(f1)) != EOF)
   putc(c,f2);
   fclose(f1);
   fclose(f2);
   printf(“after copying the data in file2.txt file is \n”);
   f2 = fopen(“file2.txt”, “r”);
   while((c = getc(f2)) != EOF)
   printf(“%c”, c);
   fclose(f2);
   getch();
}

Input & Output:

Enter the data to the file1.txt file 
STUDENT BOX OFFICE.IN
After copying the data in file2.txt file is
STUDENT BOX OFFICE.IN