 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 count the number of characters or number of words which are available into a file using file handling concept in C language. Before start, if you go to detail or description of this video, you will find links of various files related to C language. So you can also follow them. Now I am going to write the solution of this problem. So first of all, you need to declare a file pointer so that you can open a particular file to do this operation and one character variable and two in these variables one is C and another one is W. Now I am going to open a file so fp equals to f open. So let's say name of file is data.exe and I am going to open this file in read mode. Now after opening this file, we need to check this condition. So if file is not available, fp will be containing null. So it means we need to print an error message that file not found and we need to terminate the program. So you can write printf and the message file not found and then you can write exit. So exit will terminate the execution of the program and if file pointer is not equal to null, then the further statements which are available will be executed. So now I am going to write an infinite loop because I don't know how many characters are available into a file but whenever we have a file, so let's say this is a file and it is having at the A, B, then space at the right hash then is and then E, O, F. So this is the end of file notation. So these all are the characters and after completion of all the characters, file always contains end of file. So we need to check whether we are on end of file or not. So if we are on end of file, then we need to terminate this, otherwise it will iterate. So inside this loop, I am writing ch equals to fgetc fp. So fgetc will read one-one characters from this file and those characters will be stored in ch. So in first quotation, ch will be reading this A and after reading, I am going to apply C++ so that one character count will be done. Now we need to count words as well. So for counting words, I need to check spaces. So if we have five spaces, five spaces then it means we have six words. So the number of spaces plus one will be the number of words in a particular file or statement. So after this, I am checking if or maybe initially counting we can do this thing. Because it might be possible your file is blank, it is having nothing. So before counting, you need to check this. If ch w equals to end of file, then you need to break this. So that nothing will be counted. And if it is false, then you can count character to C++ and you can write if ch w equals to space, then you can count w. And then you can close your w. So this way, C is counting total number of characters and w is counting number of spaces and this loop will repeat in finite time only if this condition is true, this loop will be terminated. So after termination of this loop, we need to print the value of C and w. So you can write print f, backslash and c, c, w plus one. So C will print number of characters and w plus one print will print number of words, because if spaces are, let's say you have n spaces, so words will be n plus one. So after this, we can write f close, f p so five will be closed and the required output will be displayed on output. So this way, if you want to execute this program on laptop, so first create a file with multiple words and then put that name here. And if you have a particular path, let's say C drive, then you have a particular folder and name of file, so this you guys follow here so that a particular path, from a particular path, your file can be read and then this condition will be checked and accordingly your output will be displayed on output. So this is very easy program, you can implement it so that you can understand how file handling works in C language. And if you want to watch more programming related videos, so at the end of this video, you will find links of playlists. Also, those links are available in the description of this video, so you can follow those playlists so that you can understand C language properly. Thank you for watching this video.