Files in C | C Language Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to nourish technologies this is cynnwys stray we are going to discuss about file handling concept in a c language using c library c programming language so we can work with files also how to read the information from a file write how to process the information how to delete the information all these operations we can perform using c language all the predefined functions available in a library write how files files in c or file handling in a c language so first of all if you want to work with the file so generally if you want to work with the integer data so we need to use an integer pointer or if you want to work with a float data so we need to use a float pointer in the same way now if you want to work with any file right here it is our requirement is a file pointer so first we need to create a file pointer what is the syntax syntax it is a predefined data type it is a file all are capital letters file pointer and this is identity so identity is important file pointer and see the example example file file pointer any name you can give for example PTR so variable name is a PT R so here PTR is a pointer it can points to any type of fine PTR can points to any type of file it can hold the address of the file it can hold the address of the file so this is a file pointer declaration with the help of this pointer only you can process the information of the file how to open a file we'll see so function is a F open function f open function this function is used to open a file use it to open a file in different modes in different modes sir what are the modes read mode write mode append mode different types of file modes are available so we can open in any mode if you want to open we should go for F open function only no other function and what is the exact prototype of F open function what it will do see the prototype of F open function prototype function name is a F open function name is F open it is taking the file path so what file you want to open the file path you should provide the path we should provide in the form of string double quotes and next one mode also you should provide that is also in double quotes nothing but character pointer type string type all right path and mode on success on success so what it will do means it opens a file and returns a pointer to file address of file if file is present what this F open function will do means first it will open the file after opening the file at a particular memory location the file will be opened generally the address of the file will be written on success it will written address of file for any reason if it has failed for example file is not present file is not present on failure it returns null pointer it returns null pointer so what is the return type of F open function it is a pointer type which pointer file pointer type return type of F open function is a file pointer right so here F open function is taking two arguments first one is what is the path of the file second one in which mode you want to open the file two things and here it is on success if successfully file is open so then it will written the address of the file on failure for any reason suppose if it couldn't open the file then it will return null pointer okay now we will see one example that how to open a file and how to check whether the file is present or not so how to open a file right we'll see the program in the main see all the file related functions belongs to stdio dot H header file only if you want to include you can write that statement hash include STD I would not hedge STD I go dot edge all the file handling related functions belongs to a standard input/output header file only so now first I am creating one file file pointer any name suppose FP I had given F P then FP gets memory allocation FP get memory allocation at some location at some location so now I want to open a file so what is the function we need to call F open function we need to call F open path we have to provide sir what is the path any drive inside the drive any folder for example right into my system J drive is there in Z Drive some examples folder is there in examples folder some test dot txt file is there text dot txt file I want to open this file test dot txt file sir in which mode in a read mode I want to open just a character are you have to pass our represents a red boat but you MA to place in double quotes it is a character pointer type nothing but it is a string type alright in J drive in an examples folder test dot txt one file is there right suppose if file is present in as in here what will happen here it will open the file it will open the file so this is test dot txt file test dot exe some information is there information is present information then it will open right it will open at some location memory location and that address will be returned to FP so that is we are collecting into FP so that it is not pointing to the first character of the file first character so this is pointing of your file see here returned value we are collecting into a pointer FP collecting into pointer FP and next we are checking if file is successfully opened or not if file is open successfully so then only we can perform any operations for example here we are opening in a read mode we can read the information from that file right if file is not present for any reason if it has failed to open then we cannot work with that file so here first we need to check if file is present or not how can we check on failure it returns a null pointer so if ft equals to null that means file not opened file not open so here it is we are printing the message printf printf file not present a message we are giving very clearly file not present suppose if FP is not equals to null then else block execute then here it is this message will print printf file opened in read mode this is opening the file opening the file this is a simple program that represents how to open a file in read mode using F open function with the help of pointer and with the help of F open function we are opening the file this is a file opening process okay so now we will see once you open the file how to read the information from that file how to read the information from the open file so now we are reading the information from the file sir in how many ways we can read the information we can read a character by character we can read string by string right we can read the integer by integer or different types of data we can read at a time so first we will see how to read the data from a file a character by character okay so read character from a file so how to read the character from a file so one method they have given so what is the method F get C method so getting character from the file F get C method it is taking two arguments so first argument sorry it is taking only one argument so what is that argument it is taking means the file stream from which file you want to read the file pointer you should pass as an argument this is the prototype right here it is on success or on failure what are the values it will return return type is a integer why on success if it is successfully read if it is successfully read any character from the file written ASCII value of character ASCII value of character it will return for any reason if it has failed on failure it returns -1 that is also called a way of end of the file end of the file is a macro as a macro is nothing but a preprocessor concept right it is a con tent value it holds the value minus one it's just a variable on failure it returns -1 so now based on these two things only we have to read the entire content all the characters of that file into our cyst into our program how to read the information once again the method name is or the function name is f get C getting character from the file right if we need to pass the file pointer from each file you want to read the information the file pointer you should pass and it will start reading character by character from the file on success it returns ASCII of character for any reason if it has failed on failure it will return end of file that is minus 1 sir what is that end of file what is the character of end of file means it is a blank I will show you practically okay see how to read the information from the file how to read the information from the file observe here here for example how many characters a file contains that we cannot understand exactly how many characters a file contains that means how many times we have to call F get C function that we cannot understand sir then how can we read all the characters of the file now observe first I am declaring 1 integer variable wiser because the written type of F get C function is a CH now how many times we do not know so repeat 1 while loop or any loop any loop sir which function we need to call a function is a F get C function we need to call sir what we need to pass so what we have to pass the file pointer that where you want to read from simple for example file pointer FP equals to F P equals to F open I want to read the data for example G in the g-drive testa dot txt file is there and I am opening in a read mode now it will open the file in read mode and here it is FP is pointing to this file so I want to read the information of this file so what we have to pass FP we have to pass FP if you pass the value FP then f gets is start reading the information from the FP right how it will read a character by character it will read the first character of the file is a what is the first character of the file is a it will read here it is the file character is a a the first character it will read right character corresponding value small a corresponding value 97 it will written that we are collecting into CH because on success it returns the ASCII value of that character ASCII value of a small year is water 97 that will be collected into CH and we need to check whether it is equals to end of file or not nothing but minus 1 or not if it is not equals to end of file means what the control not reaches the end so we should continue so control come inside we are printing that character printf percent is see that character we are printing do not give a percentage D if you give percentage D instead of printing the first character a it will print the 97 so directly provide that format specifier is a person is C okay and next after reading this automatically the loop will repeat while loop as long as the condition is a true it will repeat next to be D value 98 it will return 98 not equals to minus 1 condition true and it will keep on reading all the characters from the file whenever it reaches the end of the file it will reaches the end of the file at the end it will return minus 1 so minus 1 not equals to minus 1 condition failed then it will terminate so this is the logic to read the information from the file here it is first F get C of F P it will read 1 character from the file right what character it has read the ASCII value we are collecting into ch so that CH is of type in teaser type and it is checking whether it is reaches end of the file or not if it is not reaches end of the file we are printing that character again we are repeating until it reaches end of the file this is how to open the file in read mode what are the things required to open a file in read mode and how to check whether the file is present or not and after opening how to read the contents of the file okay in the next session we'll discuss some more examples on a file handling in C concept for more videos please subscribe to nourish 80 channel thank you
Info
Channel: Naresh i Technologies
Views: 426,136
Rating: 4.856626 out of 5
Keywords: C Language, Files in C, Srinivas, Naresh IT, Learn C Language, Hands on C Language Training, C Language Demo, Online C Language Training, C Language Tutorial Videos, C Language Overview, C Language Interview Questions
Id: _KW_YBTXhN0
Channel Id: undefined
Length: 16min 36sec (996 seconds)
Published: Fri Sep 30 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.