 Hello friends, I am Sanjay Gupta. In this video, I am going to demonstrate you the use of fwrite function to write an array into a file in C programming and the use of fread function to read an array from file in C programming. Before starting, you can note how you can search for 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 first I am going to demonstrate you the use of fwrite. After that, I will be demonstrating the use of fread function. So first of all, I am including two header files, stdiv.h and second one is stdiv.h. Now I am going to define main function. Inside main function, I am declaring a file pointer that is fp. Then I am declaring an array a whose size is 10 and a variable i. Now I have to open a file in which I am going to store the array. So name of file is array.txt and I am going to open it in wb mode. So w is for write and b is for binary. So remember that whenever you are using fwrite and fread functions, you have to read or you have to write data in file in form of binary. Now I am going to check whether this file is opened or not. So with the help of this if condition, I am going to check whether the file is opened successfully or not. So if this condition is true, it means fp is equals to null. It means file pointer is containing null value. So file is not opened successfully. Otherwise it is opened successfully. So if file pointer is containing null, then I can print the message error cannot open file and I can call exit function which terminates execution of the program. So this way I have checked whether the file is opened successfully or not. Now I have to read an array from user. So for that purpose I am using this printf function which will display the message enter 10 elements. Now to read 10 elements from user, I am going to implement a for loop and with for loop the statement scannert will repeat and the statement will be reading 10 elements which will be entered by the user. So after completion of this loop, array A will be containing 10 elements which are entered by the user. Now I have to store this array into the file. So I am calling f write. Here I have to pass four arguments. So this way I have passed four arguments in f write writes array into file. So here you can see first argument is name of array. So this array is containing all the elements. Second is size of int. So size of int means size of a particular block which is available in array. Third argument is 10 which is denoting total number of elements which are available in array and fourth argument is file pointer in which array will be stored. So this way if you want to write an array into a file, you have to pass these four arguments then I can call f close fp. So this way I have stored the array into the file with the help of f write function. Now I am going to execute this code and this array.txt will be created automatically because I am using wb mode and after opening the file the data will not be in readable form because I am using binary mode but I will be reading that file with the help of f read function so that we can check whether the array is stored properly or not. So first I am going to execute this code. You can see it is asking for 10 words. So I am entering 10 different numbers. I have entered 10 numbers and the program is executed successfully and a file array.txt must be created automatically. Now we have to check this file. So this file will be available at this path that is ctc and bin because the source code which is stored in s.c file is also stored in ctc bin folder. So now I have to move to bin folder so that I can check whether the array.txt is available or not. So here you can see I am in bin folder which is available in c drive tc folder. So inside bin folder you can see array.txt file is available. It is created automatically. Now I am going to open this file so that we can check whether it is containing elements or not. So you can see it is containing some values which are in form of binary. It is not readable. So I already told you that the data will be stored in binary format because I am using mode wb. So now I am going to change this code for f read function. So this code is reading values from user and it is writing array into file. Now I am going to change this mode to rb. It means file will be opened in read mode. Now in place of f read I have to use f read. So this function reads array from file. So arguments will be the same. First is array. Second is size of each block. Third is total number of elements and fourth one is file pointer. But this time file pointer will be providing 10 elements which are available in the file to this array. Now we can print this array so that user can see what elements are available in this array and in file. So this message will be displayed on output screen and through for loop I am going to display the elements of array on output screen. So this way through f read all the elements which are available in a file will be received in array first 10 elements then through this loop all the 10 elements will be printed on output screen. So now I am going to execute this code. You can see it is opening file in read mode. It is reading array from file and then it is displaying the array on output screen. It means I don't have to enter anything while executing this code. So now I am going to execute this code. You can see the output. It is showing all the elements in form of array which are available in a file. So this way I have stored 10 elements in file with the help of f write. Then I have read those 10 elements through f read function from file. So these are two different programs which I have demonstrated you first you have to execute f write then you have to execute f read because f write will be storing data into file then only you can read that data from the file with the help of f read. So I hope you have understood how we can use f write and f read functions for reading and writing arrays with the help of file handling in C programming. 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 there. Thank you for watching this video.