Here are the details of the process:
1. Include header files: The main reason to include or use stdio.h in C is that the header file imports different macros, variables and functions to perform input, output, exit and file copying operations.
So that we need the stdio.h header for file operations:
#include <stdio.h >
#include < stdlib.h >


2. Source and destination file:
Declare variables for source file name and destination file name to store the file names. i.e. the source file that we want to copy (source) and the destination file that we want to create a new file . You can ask the user for define them or input directly in code.
char src_filename[80];
char dest_filename[80];


3. Opening the source file:
Use fopen function to open the source file in read mode ("r"). This function returns a pointer to the file object (FILE *). Check if the file opened successfully using the return value.

FILE *src_file = fopen(src_filename, "r");
if (src_file == NULL) {
printf("Error while opening source file!\n");
return 1;
}


4. Opening the destination file:
Use fopen again to open the destination file in write mode ("w"). This creates a new file if it doesn't exist, or overwrites any existing file with the same name. Handle errors similarly to the source file.

FILE *des_file = fopen(dest_filename, "w");
if (dest_file == NULL) {
printf("Error while creating destination file!\n");
fclose(src_file); // Close the source file
return 1;
}


5. Copy the contents:
Use a loop to read characters from the source file one by one using fgetc. If the end of the file (EOF) is not reached, write the read character to the destination file using fputc.

int ch;
while ((ch = fgetc(src_file)) != EOF) {
if (fputc(ch, dest_file) == EOF) {
printf("Error while writing to the destination file!\n");
break;
}
}


6. Close the files:
Use fclose to close both the source and destination files to release resources.
fclose(src_file);
fclose(dest_file);


7. Success message:
Print a message to the user indicating successful completion.
printf("Your File has been copied successfully!\n");


Put all together.

C program for copying content of one file to another file.

#include <stdio.h>
#include <stdlib.h >
int main()
{
 char src_filename[80];
 char dest_filename[80];
 printf("Enter the source file name to open for reading \n");
  scanf("%s", src_filename);
 FILE *src_file = fopen(src_filename, "r");
 if (src_file == NULL)  {
    printf("Error while opening source file!\n");
    return 1;
 }
 printf("Enter the destination file name to open for writing \n");
  scanf("%s", dest_filename);
 FILE *dest_file = fopen(dest_filename, "w");
 if (dest_file == NULL)  {
    printf("Error while creating destination file!\n");
    fclose(src_file); // Close the source file
    return 1;
 }
 int ch;
 while ((ch = fgetc(src_file)) != EOF)
 {
    if (fputc(ch, dest_file) == EOF)
 {
    printf("Error while writing to the destination file!\n");
    break;
  }
 }
 fclose(src_file);
 fclose(dest_file);
 printf("Your File has been copied successfully!\n");
 return(0);
 }


Previous Topic:-->> Count word,char,line in Files. || Next topic:-->>Update file Content.