 Hello friends, I am Sanjay Gupta. In this video, I am going to demonstrate you how you can merge contents of two files into another file or you can say into third file using a C program. Before starting, you can know how you can search my YouTube channel. You can type my name Sanjay Gupta in YouTube. My channel will be available there. You can watch various programming related videos here. Now I am going to implement solution of this problem. First I am going to include a header file stdio.h then I am including another header file that is stdlib.h. Now I am going to define main function. Inside main function, I have declared a file pointer and a character variable that is ch. So here I have to use three files. So I require three file pointers here. So I am declaring f1, f2 and f3 file pointers. Now first I am opening first file whose name will be file1.txt and I have to open it in read mode. Then I have to open second file and this file is also opened in read mode. Then I am opening third file that is file3.txt and I am opening it in write mode. So I have to copy contents of file1 and file2 into file3. So now I have to check all these files whether they are open successfully or not. So I am applying these three conditions f1 w equals to null or f2 w equals to null or f3 w equals to null. So if any of these three condition is true then an error message will be displayed on output screen that cannot open file and through exit the program will be terminated. If this condition is false it means all three files are open successfully then I can shift or I can merge contents of file1 and file2 into file3. So first I am going to copy contents of file1 into file3. So I am calling fgetc function and I am reading content of f1 file into ch variable. Now I have to check whether this ch is equals to end of file or not. If it is equals to end of file then I have to break the loop because end of file means file is terminated and it is not containing any content after this point. So if ch is equals to euf then loop will be terminated otherwise with the help of fputc I can write the received character into f3 file. So this way the contents which are available in file1 will be received through f1 pointer into ch variable and those characters will be stored inside f3 pointer with the help of fputc function. So this loop will copy file1 into file3. Now after completion of this loop I can close file pointer1. Now I am copying this loop and I am pasting it. Now this loop will copy file2 into file3. So here I am writing f2 file pointer. So the contents of file pointer2 will be received through fgetc into ch then it will be checked for end of file if it is true, if this condition is true then loop will be terminated otherwise ch will be stored inside f3. So this way through these two loops contents of file1 will be shifted first into file3 and then content of file2 will be shifted into file3. Now I can close f2 file pointer and f3 file pointer and at last return 0. So now I have to create two files first file1.exe and file2.exe because I am going to open these files in read mode. So these files must be available in a folder and file3 will be created automatically because I am using w mode. So now you have to focus that I have saved this source code with test.c name in ctcn bin folder. So I have to create those notepad files file1 and file2 in ctcn bin folder. So I have opened notepad. Here I am typing sanjan and I am going to save this file in ctcn bin folder with name file1.exe. So you can see this file is saved with file1.exe name. I am closing this notepad. I am again opening notepad and this time I am typing gupta and again I am going to save this file on same path ctcn bin with name file2.exe. So you can see the name of file it is file2.exe. So I have created two files. Now I am moving to c drive tc folder and bin folder. Here you can see both the files are available file1.exe and file2.exe. I am opening file1.exe it is containing sanjan. I am opening file2.exe it is containing gupta. But file3 is not available here. Now I have to execute this code so that merged content will be available into file3.exe. So I am executing this code. Program has executed successfully. It is not showing anything on output screen because the contents are stored inside file. Now I am moving to bin folder. Here you can see file3.exe is created automatically. Now I am going to open this file. You can see the contents. Sanjay Gupta is available here. Sanjay was stored in file1 and Gupta was stored in file2. But both the contents are merged in file3 with the help of this c program. So I hope you have understood how we can merge contents of two file into third file with the help of c program in file handling. I hope you have understood the concept well. If you want to watch more programming related videos you can search my name Sanjay Gupta in YouTube. My channel will be available there. You can watch various programming related videos here. Thank you for watching this video.