 Hello friends, I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I am going to explain how you can copy or how you can concatenate two files. So first I will be explaining how you can copy the content of one file into another and then I will be tell you how you can concatenate data of two files. So before start, if you go to description of this video, you will find links of various playlist. Those are available. So those are related to C language video. So you can follow them. Now I'm going to explain this first how you can copy content of a file into another in file handling in C. So here you can see I'm going to declare two file pointers. First one is F1, second one is F2. So F1 will belong to a particular file and F2 will belong to another file. And after that I am declaring a character variable. Now with F1, I am going to open, let's say data1.txt. This I am going to open in read mode because I am going to leave the data which is available into this file. And I am going to open another one as data2.txt and it will be opened in w mode. So here you need to focus. Data1 should be available. If it is not available, then there is no need to copy the data. So that's why it is in read mode so far. You need to create this file on a particular path and here you can place that path like this. So first you need to mention the drive. Their file is available then folder hierarchy and then name of file. If this file is available then we can only copy content into this and here for second file I have created this file with w mode. So w mode says if file is not available it will be created automatically. So again instead of writing data2.txt you can also write fully qualified path here so that the file which is present in a particular location its copy will be created on that location. Now after writing these statements we can write F1 w equals to null or F2 w equals to null. So in case if any of the pointer whether it is F1 or F2 is having null it means we are trying to open two files but if anyone is not open with any reason then there is no need to go further. There is no need to execute further statements. So if this condition is true you can write files not found or you can write exit and close this if condition. So if F1 file pointer is equal to null then it will be executed. If F2 is equal to null then also it will be executed and if both are not equal to null then it will be false and further statements will be executed. So this if condition is very important because if file is not available then there is no need to apply the copy of a file into another operation. Now if this condition is false it means both the files are available. Now we can write the statements so that we can copy the data from one file to another. So I am writing a loop y1 so whenever we put 1 into a yu so it means we are iterating this loop for infinite times. Now I am writing this ch equals to fgetcf1 so let's say this is data1.exe which is having my name stored and this is another file data2.exe which is blank. Now if we execute this statement so fgetc is a predefined function which leads a particular character from file and which file f1. f1 is linked with data1.exe so first character that is s will be read and that will be stored into ch variable. Now one more thing that you need to know after each file eof that is equal to end of file is automatically available. So in string you might have used this null so null is automatically available after or end of each string. So similar to that in each file at the end we have one value that is eof end of file so while reading this you can apply this if ch is equal to end of file. So eof is also a character if it is equal to this so you can break the loop and if it is false then you can write f put c the character and set it in file. So this is a small loop which is responsible for copying the content of a file into another so this loop is repeating infinite time but this if condition is making it finite. So if character is equal to end of file so it will be terminated otherwise the character which is received from f1 into ch that will be stored through f put c into f2 file. So one by one each character will be copied into second file and whenever we will be receiving eof this loop will be terminated and end of file will be inserted automatically into . And after this you can write f close all so f close all is a function which closes all the open file in the program. So we have open two files so both will be closed automatically with this function. So this way I hope you understood how we can copy a file into another. Now our requirement is to concatenate after understanding the copy process we need to understand the concatenation preference. So this is also very easy with the help of this program I am going to explain you how we can concatenate two files. So let's say this is having Sanjay and this file is having Gupta and eof and we need to concatenate both Sanjay and Gupta together into a file. So let's say I want to concatenate data into this file so after Sanjay Gupta will be available. So I need to read this file in R mode and this file I am going to read in A mode. A mode is for appending so I am going to append data into data1.exe and reading data from this so that it will be appended here. So now I need to read data from F2 which will be stored in CH and here I am going to use F1. So F1 is opened in up end mode so initially file pointer will be available at the end of file and the last name which is stored in data2.exe will be stored after the first one. So this way by minor changes by converting W mode into A mode we are able to concatenate two files. So W mode always opens a blank file and A mode keeps the existing data as well and it allows you to write data at the end of file. So this way you can copy and concatenate two files in file handling so I hope you understood both the processes how we can do this. So try to implement this program in your laptop so that you can understand the process and I hope you understood whatever I explained in this video and if you want to watch more programming related videos at the end of this video you will find links of various playlists also in the description of this video you will find links of various playlists related to C-levels. So follow them and thank you for watching this video.