File Handling in C Programming Language Video Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is Anil with a video tutorial for the learning lad on C programming and this video tutorial we're going to see the files in see a files are the building blocks of every operating system we can say that files are the virtual container to store some data so in other tutorials we have used variable to store data so when our program terminated then the value stored in that variable we are lost so if you want to save something in the memory permanently then you can this files and store it so let's see how we can use these files in C so to use a file in C first you have to open that file so for that purpose this stdio dot H header file provides a function for you and the function is called F open as a parameter to this F open function first we have to specify the file name that we want to open then the mode at which we want to open so this file name is a name of the file with the extension so if you open a text file then you have to specify file m dot txt if your opening are the set dot C file then you have to specify file name dot C so then the mode is pretty much like what you want to do with that file so if you want to open the file for reading then you can specify this more as our our for reading and make a note that you have to specify this mode in double quotes so you can specify this mode in single quotes that's because you know this mode is a string value now when it's F open function open this file using this mode our first is can assert for that file so if that file is present then is going to open that file for you if that file is not present there then it's going to return a null value then if you want to open the file for writing then you have to specify this mode as W so when you open this file with the mode W this F open is conserved for that file inner disk so if that file is that it's going to delete that file and it's going to recreate that file for you so so the contents in the file will be lost so it's going to be an empty file so if your file is not present in a disk then this F open is going to create that file and open it for you so the next thing is if you want to edit or append to that file then you can open it with the mode a so when you open this file with a mode a if their file is there then this F open is going to open that file and the contents of that file will not be lost it will be present and if you want you can edit to that file if your file is not that then this F open is going to create that file for you so the next mode is a plus so if you use the MOR as a plus then you can open that file for reading and writing purpose so similar to our if that file is not there then it's going to return a null value so if their file is present then it's going to open that file for you so the next one is w+ so similar to w if that file is there then the contents of that file will be lost and you'll be presented with an empty file and you can edit read and write from that file is in this w+ so the last one is called a plus so when if you use this more as a plus then if that if your file is present in a disk then it's going to open that file for you for reading and writing and the contents of that file will not be lost and it will be there and you can add it to that file so if your file is not present in that disk then that file is going to be created and open for you so this is about the normal file so then if you want to work with a binary file then you have to suffix it with the be here that's for indicating that ok I am working with a binary file it's going to be our B WB a B then a plus B W plus B and a plus B so the next thing is when we work with the file you we have to remember a lot of things for example the file name the mode at which we have opened the file then we have to remember last byte or last word which is read from the file last byte or last word which is written to the file and many things so for the purpose you know this stdio.h header file provides a structure for us and the structure is called file so when we use this F open function to open a file with a specific mode if that file is present in a disk then this F open is going to return the address to this structure file so that structure is going to contain all the information about that file since this F open is going to return the address of the structure of type file we need a pointer of type file is going to be star let's call it f pointer so now I am going to delete all these things and this tutorial for the demonstration purpose we just gonna create a text file so to create a file we have to open it first it's going to be F open then the file name we call it as a nil dot txt and the mode so since we are running this program first time you know the file will not be dead so that's why we want to create that file that's why I am going to specify this more as W so then this F open is going to return the address of the structure which contains all the information about this file that is why we have to as in it without with this point of F pointer that's why I'm going to write f pointer equal to F open of onion dot txt and W so if this F pointer is contains some value or some address then we can say that that that file which we specified here is opened successfully so if this F pointer contains a null value then we can say that this F open unable to open the file so we can test that by using if condition if F pointer equal to equal to null then we can say that printf unable to create the files then else that means you know this F point I contain the address of the structure and we can say that file opened successfully so now the last thing we have to do is when we open that file we have to close it that is why here we're going to close it it's going to be F close the function name then we have to pass the pointer it's going to be F pointer so this is it the next thing I want to explain here is you know this v dot C is present in my desktop so when I compile this program you know files dot exe will be created at the desktop only so when we run our program that is when we run our file dot v dot exe you know this Annelle dot txt file will be created at the location where that file dot exe is present in this case that files dot exe is present in my desktop that is why this unalloyed txt is going to be created in the desktop so save it compile it and run it so as you can see here file open successfully that means that you know this F open function created a file for us successfully so now I'm going to minimize it and open up my desktop so this is my desktop as you can see here my files dot C and files dot exe is present in a desktop that's why this uh nil dot txt is created in the desktop and I'm going to open it so as you can see here it's an empty file here another thing we want to do is we are going to delete this file now now uh nil dot txt is not present and for the demonstration purpose we cannot make it R now we are opening the file with a mode R so when we use this mode our if that file is present then only this F pointer is going to return the structure address so now that file is not present we have deleted that file that is why this F open is going to return a null value that is why we can will get unable to create the file so we are going to save it compile it and run it so as you can see here unable to create the file so similarly we are going to change it to a save it so now you know this file will be created for us similar to W save it compile it and run it so as you can see file open successfully I'm going to close it and I'm going to open up my desktop so as you can see here you know this uh nil dot txt is present so I'm going to open it so this is it this is about the files and see in the next tutorial we're going to see some more information about the files like how we can edit some contents to that file and how we can read from that file so thanks for watching and don't forget to subscribe
Info
Channel: LearningLad
Views: 161,671
Rating: 4.8154464 out of 5
Keywords: file, c file handling, c file handling tutorial, file handling in c programming, file management in c, c file io, c file processing, file operations in c, c language tutorials for beginners, C (Programming Language), Computer File
Id: wVDfRzBp8iE
Channel Id: undefined
Length: 9min 58sec (598 seconds)
Published: Fri May 03 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.