C program to copy contents from one file to another by unusual technician

c program for copied content from one file to another file

    
#include <stdio.h>
#include <stdlib.h> 
 /* include for use exit() function */
#include <conio.h>
int main()
{
	char ch, SRC[100], TRGT[100];
	FILE *SRC_FILE = NULL, *TRGT_FILE = NULL;

	printf("Enter path of your file to copy \n");
   
    
   
   /* if this software have in same directory
     then you can enter only name */
	
    gets(SRC);

	SRC_FILE = fopen(SRC, "r");

	if (SRC_FILE == NULL)
	{
		printf("something wrong \n press any key o exit");
		exit(EXIT_FAILURE);
        /* EXIT_FAILURE is  macro 
        declare in stdlib.h passing for unsuccessfully 
        termination  */
	}

	printf("Enter path of your target file \n"); 
	gets(TRGT);

	TRGT_FILE = fopen(TRGT, "w");

	if (TRGT_FILE == NULL
	{
		fclose(SRC_FILE);
		printf("something wrong \n press any key o exit");
		exit(EXIT_FAILURE);
	}
	while ((ch = fgetc(SRC_FILE)) != EOF)  
    /* EOF is a macro with value -1 
    fgetc() return it when all data are finished */
	{
		fputc(ch, TRGT_FILE);
	}

	fclose(SRC_FILE);
	fclose(TRGT_FILE);

	printf("file copied successfully");
	getch();
	return 0;
}
    

Simple explaination of c program to copy contents from one file to another file

for this problem we use file handling in C programing. for better explaination we can take example of code given upside. in this program we declare two char arrays (SRC[100], TRGT[100]) in which we can store path of file. if the file have in same directory then we can give only name. then we declare two pointers of FILE type (*SRC_FILE = NULL, *TRGT_FILE = NULL) in which we store address of file with the help of fopen() function. fopen() function load the file in RAM then return it's address otherwise return NULL. that's why it's necessary to check fopen return value is not NULL because from this we know that file open or not. after open first file then we pass path of second file to fopen function in which we want to copy first file data. for coping all data we run while loop in which we bring character one-by-one by using fgetc function from first file and we paste that data in second file by using fputc function. we run this process untill that time when fgetc function does'nt return EOF. EOF is a macro whose value is -1 it's rreturn by fgetc function. when all the data is finished.