 Hello friends, I am Sanjay Gupta. In this video, I am going to demonstrate you the program which will read a number from user. It will calculate factorial of that number and the result will be stored into the file using file handling in C programming. 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 the solution of this problem. So first I am including a header file that is stdio.h. Now I am including another header file that is stdlib.h. Now I am defining the main function. Inside the main function, I am declaring some integer variables like n, i and f and I am initializing f with 1. Now I am declaring a file pointer that is fp. So this is file pointer. This file pointer will be required to open a file. So fp equals to f open. Now I am typing name of file as fact dot txt and I am opening it in w mode. So w mode opens a file. If this fact dot txt is not available, w mode will be creating a new file automatically. That's why I am using w mode and I have to write something into the file. So w mode is required only. Now to check whether file is open successfully or not, I have to write this if condition. So if fp is containing null, then I can print the error message file cannot be opened. So this message will be displayed on output screen if fp is containing null and then I can call exit. So exit function is called here because it terminates execution of the program. So if file is not open successfully, then only exit will be called. Otherwise program will be executed completely. So now after this if statement, I am writing printf which will display the message enter a number to read that number. I am writing scanf percent d then m percent n. So this way the number whose factorial I have to calculate is received inside n variable. Now through for loop, I can calculate factorial of the number inside this loop. I'm writing f equals to f into i. So this for loop will be calculating factorial of n and the result will be available inside f variable. Now to store that result into file permanently, I'm calling f printf. So here first argument is fp, which is having address of the file that is fact dot txt. Then in double quotes, I'm writing factorial equals to percent d and then f. So this output factorial equals to and the result which is stored inside f will be stored inside the file. And to close the file, I'm calling f close fp. At last I'm writing return zero. So this way I have implemented the complete code in front of you. This program will be creating fact dot txt and the result will be stored inside this file automatically. Now first we have to check whether this file is available in system or not. So the location will be c, tc and bin. So I have already opened this bin folder. You can see the location c drive, tc folder and then bin folder. Now I'm pressing f. So you can see the files related with f name are available here and here fact dot txt is not available. So now I'm going to execute this code. So we will check whether file will be created automatically or not. So I'm executing this code. It is asking for a number. I am entering five. So I have entered five and it has not shown anything because the output is displayed into a file. Now again I'm moving to bin folder. Now here you can see fact dot txt is available now and it is created automatically. You can also see the date. Today's date is 28, 3 and 2018. So it is created this time automatically. Now I'm opening it by double clicking it. You can see the output factorial equals to 120. So the output which is generated through the c program is available in this file. So I hope you have understood how I have stored calculated factorial into this file. Now if I execute this program again and this time I'm entering w. So it is executed successfully. Now again I'm moving to bin opening fact dot txt. You can see this time it is having factorial of four. So factorial of five is not available because I have used w mode. So w erases previous contents of the file and it stores new content into the file. Now if you want to store previous content as well as new content, you can prefer a mode. So now the file is containing factorial equals to 24. Now again I'm going to execute this program here. I'm using slash n so that output can be stored in different lines. I'm executing this code and I'm entering five. So let's see what happened with the file. So this time again I'm opening fact dot txt. You can see it's contains it is having two statements factorial of four and factorial of five. This is because of a mode. So I hope you have understood the difference between w and a also. So this way I have read a number from user. I have calculated its factorial and I have stored the result into the file using f print f function. So this is all about this problem. I hope you have understood whatever I have explained to you. 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.