 Hello friends, I am Sanjay Gupta. In this video, I am going to demonstrate you how you can copy contents of a file into another using a C program. Before starting, you can note how you can search my YouTube channel. You can type my name Sanjay Gupta in YouTube search. My channel will be available there. You can watch various programming related videos here. Now I am going to implement solution of this problem. So first I am going to include two header files stdio.h and second one is std linv.h. Now I am going to define main function. Inside main function, I am going to declare two file pointers. First one is f1 and second one is f2 and a character variable that is ch. Now I am going to open first file using f open function. Name of file is contained.txt. I am going to open it in read mode. Now for f2 pointer, I am going to open another file whose name is contained1.txt and I am going to open it in write mode. So there are two files contained.txt and contained1.txt. I am going to copy data of contained.txt into contained1.txt. After opening both the files, I have to check whether these files are open successfully or not. So for that purpose, I am applying these conditions. If f1 is equal to null or f2 is equal to null, then I have to show the error message that is cannot open file and I am calling exit function. So if file is not open successfully, then this error message will be displayed and program will be terminated. If files are open successfully, then I can copy contents of one file into another. So for that purpose, I am implementing an infinite loop whose condition is while1. So this is an infinite loop because I don't know how many characters are available in the contained.txt. So this loop will be repeated infinite times but to terminate this loop, I have to implement a particular condition. So first I am going to read contents of first file through fgetc. So fgetc will be reading a character from f1 and that character will be stored inside ch. Now I have to apply termination condition for loop that is if ch equals to EOF. EOF means end of file. So if ch is equals to end of file, then I can break the loop and end of file means file is not containing more characters now. So if ch is equals to EOF, then this loop will be terminated. So now this loop is converted into finite loop. And if ch is not equals to EOF, then I can transfer that character into second file that is contained 1.txt. So for that purpose, I am calling fputc. I am passing first argument as ch and second argument as f2. So this way I have implemented this loop. fgetc will be reading a character from f1 that is stored inside ch. Then that ch will be copied into f2 with the help of fputc. So this way content of f1 will be copied into f2 file. After completion of this loop, I can close f1 file pointer and f2 file pointer so that opened files can be closed successfully and then return 0. Now, first of all, I have to check whether content.txt is available or not because I am going to open it in read mode. So you can see the path of this source code ctcbin then test.c. It means in bin folder this content.txt must be available. So I am going to open bin folder. So path is ctcbin you can see here and inside bin folder content.txt is present. It is having the text my name is Sanjay Gupta but you can see content.txt is not present in the complete folder and it will be created automatically because for content.txt I am using w mode. Now I am going to execute this code. It is executed successfully. It is not showing anything on output screen because I have not used any function which will display text on output screen but files have been copied. To show the proof you can see here now two files are available content.txt and content 1.txt. Now I am going to open content 1.txt it is having the text my name is Sanjay Gupta. So now I hope you have understood how I have copied content of one file into another. Now I am going to modify this program. Here one more file is available that is data 1.txt it is having Sanjay. So now I am changing source file name that is data 1.txt. So this time data 1.txt contains will be copied into content 1.txt and for content 1.txt I am using w mode. This time file is present but its previous contents will be erased because w mode is available here. Again I am going to execute this code. It is executed. I am moving to bin folder. Now I am again going to open content 1.txt. You can see this time it is containing only Sanjay. So this way I hope you have understood how we can copy contents of one file into another. Now I am modifying this w mode with a. So content 1.txt is containing only Sanjay. Again I am writing name content.txt for f1 file pointer. I am going to execute this code. It is executed successfully. Now let's see what will happen. I am opening content 1.txt. You can see the output. It is having both the contents Sanjay and my name is Sanjay Gupta. So Sanjay is copied from data 1.txt and my name is Sanjay Gupta is copied from content.txt. Since I have used file opening mode as a. So it preserved previous output as well as new output is copied at the end of file. So if you want to keep previous data in a particular file then you can prefer a mode. If you don't want to keep previous data then you can prefer w mode. So this way I have demonstrated you how you can copy contents of a file into another with the help of C program. If you want to watch more programming related videos you can search my YouTube channel by typing my name Sanjay Gupta in YouTube search. My channel will be available there. You can watch various programming related videos there. You can also download my programming app that is TechMS. Thank you for watching this video.