 Hello friends, I am Sanjay Bhukta. I welcome you on Sanjay Bhukta Tech School. In this video, I am going to explain the important topic of C language that is file handling in C. So in this video, I will be explaining how file handling works, why we need file handling in C language. So before starting my explanation, adjust one information. If you go to description of this video, you will find links of various languages related to C language. So open those links and you can watch various videos there. Now coming on to the topic that is file handling in C. So to understand this, let me draw a diagram. So this is first block and let's say it is representing a C program that is implemented with the help of link function. And whenever you implement any program, you save the program with .c extension and that program is known as source code. So whenever you want to implement any problem with the help of C language, so you implement your source code with the help of main function. Now after implementing the source code, suppose you have saved your file with .c extension into your PC. Now after successful or saving of the file, you will compile and execute your code. So as you compile and execute your code, it automatically links with console. Console is the output thing where you provide some input and you receive output of the process. So this console will be linked with your source code and if you want to receive input to your program, so you will be dependent upon console. And if you want to show any output to the user, so that output will be displayed with the help of console. So output will be thrown through the source code and it will be displayed on console. So you will be using two things if you are implementing any program in C. First is source code that is containing all the instructions related to C language. And second is your console that is used to maintain input and output of the program. Now suppose you completed this process and you have turned off your PC or laptop. And next day when you open your laptop or PC, so this source code will be stored permanently. This will be available. But what will happen with this console? Will it be available in front of you or not and containing the same input output generated with this program? So your answer will be no. Your console will not be displaying any input output related to this program because console stores everything temporarily. It is not a permanent storage location. So if you are executing your process, so at that point of time you can see whatever you have entering or you can see whatever your program is displaying as output. But after some time whenever you have completed or you have shut down your computer. So after that when you reboot your system, so this console will not be available in front of you. So now we need to find out its solution so that we can store input and output of the program permanently. So now the concept of file handling appears. So we need to create the third block here and this third block will be considered as file and this file will be linked with your source code. And all the input output that you are managing with the help of console, now you can manage those input output with the help of file. So you can receive input from file into your program and also you can store your output input file permanently because this file will be available on hard disk drive. And whatever will be stored in hard disk drive will be stored for permanent duration. So if your file is available permanently on hard disk, it means whenever you shut down your laptop and you reboot your laptop. So both source code as well as this file both will be stored permanently in your system. And whenever you want to open this file and you want to view the content of this file. So you can independently open this file and you can view whatever contents are available into this file. So you don't need to execute this program again and again to see the content of this file. If you want to receive input output, if you want to manage input output between these two things then only you need to execute this. Only if you want to see the contents of this file then you can directly open it and you can view. So this way these are the blocks which combinedly implements file handling in C. So if you want to see the input output immediately you can use console. If you want to store your input output permanently then you can use file. So this overall process of managing these three blocks is known as file handling in C. So now let me take a simple real-life example. So you might have visited ADM machine to withdraw some amount. So in your ADM machine the software which you see is similar to this first block. The screen of ADM machine is similar to console. And after successful transaction the data which is stored permanently in the ADM machine software is equivalent to this one. Now imagine if ADM machine is not maintaining any file. So whenever you withdraw any amount from that ADM machine. So its entry will be reflected on console temporarily but that will not be reflecting anywhere. Sorry that will be reflecting on console temporarily but it will not be reflecting permanently anywhere. So in your ADM machine a file or database is working in the background. So that whatever transaction you perform can be maintained permanently. So you might have seen we can generate these statements or detailed statements of the transaction that we do with the banks. So that will be generated with the help of this file or database. So you can also use a term DBMS in place of file because in large softwares which are implemented with the help of Java, PHP or any other programming language. So they use DBMS instead of file to store large amount of data. So if your data is permanently stored in these files or databases then only you can retrieve those information whenever you own. And console is a primary location so whatever is available on console will be appeared for a particular point of time. And whenever you eat your screen or you turn off your PC that information will be given. So I was talking about ADM machine so if you want to get any statement or detailed statement. So all the information related to your account for a particular decision is stored in a database or file. So whenever you want to get that mini statement all the stored information from that file will be received through that software and a mini statement will be generated. So this whole process is known as file handling and this file handling or database handling concept is available in all the languages. If you are working with C, C++, PHP, Java, Python or any other business scenario if you are working. So you need to maintain file or database so that you can store your transaction results permanently so that in future you can use those data and store it in a file or database. So I hope with this explanation you have understood the concept of file or the importance of file so that you can store your data permanently. Now I'm going to tell you some important points or you can say important statements that you can use by implementing programs in C languages with the help of file handling. So first step is giving file pointer so whenever you want to use file handling concept in C languages. So you need to write this statement in main function so you need to write file in capital and this is a file pointer which will store address of file. So as per diagram I created a file so that file will be created into hard disk and in hard disk each file contains a particular address so that address will be stored into this file pointer so that you can process that file to store the data and to retrieve the data. Second statement will be to open that file so to open the file you need to provide file name and its mode. Right so as a point in the main function where you need to type the name of file and you need to write the mode in which you want to open. So I'm just writing basic modes R, W, A, R is for reading, W for writing and A for writing and if you want to learn these modes in detail so at the end of this video you will find a link of playlist file handling. And in that playlist you will find a video related to different modes which are available for file handling so do watch that video to understand this. Understand will be if condition so here we need to check if fpw equals null so let's say you are trying to open a particular file and that file is not available or is not created then file pointer will be having null because if file is not available then no address will be available into file pointer. So in that case file pointer will be personal and you can write print as inside the code you can write file not found and you can use a free file function that is exit so exit terminates execution of the program. So the statements which are available after this statement will not be executed because file pointer is null and file is not open so you can print this error message file not found or file not opened and you can terminate execution of the program. And this is a predefined function so for this you need to review this handle file sdnl.h so if you are including this handle file then you can use this handle function otherwise and fourth you can use f close then you can pass file pointer also you can use f close all. So these are two separate matters so if you have only one file open through a file pointer then you can use f close everything but let's say you have opened more than one files in a C program through more than one file pointers and you want to close all those files through those five pointers then you can terminate f close all. So f close all will close all open files immediately with a single scale right so these four are additional statements that you have to use in your program if you want to implement file handling apart from that if you want to. Like anything into five you can use f print f if you want to need anything from file you can use f scanner so these are some specific functions those are related to digital writing data from file into file so if you want to learn these concepts as well so again at the end of this video and also in the description of this video you will find the link related to file handling in C. So in that place I have created all the videos separately like how f print f works, how f print f works, how we can store multiple data into files, how we can read multiple data from files, how we can find arrays structures into files, how we can read array structures from files. So in this video I just explained the concept of file handling, if you want to learn more about file handling so do follow that playlist so that you can understand the concept of file handling that and I hope you understood the requirement of files in C language program because without file you cannot store your input output permanently. So if you want to display your output or input temporarily that is controlled that you are using with all the programs but for permanent location also attacks file with the help of these statements so this way I am completing this video so I hope you understood whatever I explained in this video if you want to watch more programming related videos go to the description of this video and find the links. So thank you for watching this video.