C Programming Tutorial - 51 - How to Read Files

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
like it's a mate's in this video I'm going to teach you guys how to read from a file and actually what I want to do first is right now we just have bacon is good not very cool actually want a couple lines to read I don't know if we just have one line Dorito be kind of boring so go ahead and type I don't know two or three more lines so bacon is good arm I really like to eat a like to eat meatballs and then we'll just say cheese I don't know like a freaking haiku or something bacon is good really like to eat meat both cheese so go ahead and save this in so right now we pretty much created this file right here created it with C and last sorrel and then we edit it in notepad or any text editor you have so now let's go ahead and read it in C so the first thing remember whenever working the files is just that file pointer I'm going to name it F pointer again why not so what we need to do is read this first thing F open again just think of this function is pretty much arm the equivalent of opening up your notepad program so f open pretty much opens up its own version of notepad even though that's not what does but if you think about it that way it's going to be really easy to understand this function and of course whenever you have notepad open you need to select what file you want to work with and we're just working with this bacon text file so that's the equivalent of doing file open that first parameter and the last one is what do you want to do with it well in less oil we wrote the file and now we just want to read it you're pretty much saying whatever's in here get ready to just give me that information read it back to me now what I want to do is print it on the screen and we actually need one other thing to just print it in this little command line thing because right now here let me close this up close F pointer so right now this command line or this um yeah I guess you call it this little command line black box this is very simple program it can't just take a file and read it this actually needs variables in C that's what it was built for so in order to display the contents of our file in that little command line a window we actually need to first store store the content in arm something that C can use such as a character array so I'm going to sort in this single line array because I'm going to be printing it out line by line so once we have an array created what I can do is just make a very basic wot loop and this is how you read a file line by line now the first thing is we need to say run this loop pretty much from the beginning of the file to the end so by default remember our pointer or file pointer is going to start at the beginning so we're pretty much going to say you loop until the end how do we do that well we'll say X let me just type in then talk to you guys about FeO F f pointer and hit enter so basically what this means is file and a file in this explanation on mark you know that it means not so what this is saying is as you read the file this file pointer is going to progress like this it's going to go to the end and then start on a new line and it's going to go all the way pretty much left to right exactly how people read in real life from left to right line by line and what this is going to say is keep looping until you get to the end of the file in other words as long as the file pointer did not reach the end of the file yet continue to run this simple enough so now we can just go ahead and um the first thing that we need to do is actually say okay f gets gets one line just like it does in the command line so f gets single-line 150f pointer what this is going to do is it gets a line now before we just you'd gets gets to get it from the command line but in this case it's going to get your file line-by-line so instead of using standard input such as keyboard it's going to use that file so the first time it's going to be equal to this the second time is going to be equal to this and the last time it's going to be equal to this which is cheese so that's what F gets those and now we can just use regular puts because we're not putting it to a file we're just putting it to the command line for the user to see and of course the information was stored in single line so just print out whatever it is on the single line and that's it so let's run this and check it out so what it's going to do is it's going to go through and get all that crap line by line and then print it out on the screen so bacon is good I really like to eat meatballs in cheese now the only other thing I want to point out is this you see how there as a space between every line well the reason for that is because this remember puts automatically adds content to a new line every time so we understand why this stuff is on new line but why the space in between where is that extra new line coming from well it's coming from here in New York X file even though you can't see it your computer programs see this at the end of every line a new line character and this is how um your text editor knows to put each of this crap on a new line however let me redo that actually redo redo however what's happening in this program right here is it's copying all of this with F gets and then it's putting it to a new line so whenever it puts that to the new line it adds the other new line from puts plus the new line that was already in this text file right here so that's why you have a space between every line even though your text file up not too so again one last time if you got this down move on next tutorial basically we open the file we said okay we want to read from it using this our character and then we said okay we pretty much need to character a ready to store all the crap that you're going to read read it line by line print it out on the screen and then when we're done direct with everything close it out very simple stuff now we know how to write files and read files the only other thing that I want to talk to you guys about is how to append files how to add on to the end of this and once we're done with that it's actually going to be like two seconds we can learn about random access files and a whole bunch of other cool stuff but for now thank you guys for watching we're going to subscribe and we'll see you next time
Info
Channel: thenewboston
Views: 468,776
Rating: 4.89081 out of 5
Keywords: language, for, beginners, youtube, thenewboston, lesson, bucky, tutorial, lectures, arrays, functions, pointers, structures, bangla, roberts, how, to, learn, education, class, C (Programming Language), Computer Programming (Conference Subject), How-to (Website Category)
Id: 8nIilb2kiSU
Channel Id: undefined
Length: 7min 18sec (438 seconds)
Published: Tue Aug 19 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.