what is the process of retrieving data from a file called? This is a topic that many people are looking for. amritsang.org is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, amritsang.org would like to introduce to you C Programming Tutorial – 51 – How to Read Files. Following along are instructions in the video below:
Its a mates in this video. Im going to teach you guys guys how to read from a file and actually what i want to do first right now. We just have bacon is good not very cool actually want a couple lines to read.
I dont know if we just have one line dorito be kind of boring so go ahead and type. I dont know two or three more lines. So bacon is good arm.
I really like to eat a like to eat meatballs and then well just say cheese. I dont 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 lets go ahead and read it in c. So.
The first thing remember whenever working the files is just that file pointer. Im 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 thats not what does.
But if you think about it that way its 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 were just working with this bacon text file. So thats 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 youre pretty much saying. Whatevers 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.
I guess you call it this little command line. Black box. This is very simple program.
It cant just take a file and read it this actually needs variables in c. Thats 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 im going to sort in this single line array.
Because im 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 were pretty much going to say you loop. Until the end. How do we do that well well 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 its going to go to the end and then start on a new line and its 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 youd gets gets to get it from the command line. But in this case. Its going to get your file line by line.
So instead of using standard input such as keyboard.
Its going to use that file. So the first time. Its going to be equal to this the second time is going to be equal to this and the last time.
Its going to be equal to this which is cheese. So thats what f gets those and now we can just use regular puts because were not putting it to a file. Were 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 thats it so lets run this and check it out so what its going to do is its 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. Its coming from here in new york x. File.
Even. Though you cant 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 whats happening in this program. Right here is its copying all of this with f gets and then its 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 thats 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 youre going to read read it line by line print. It out on the screen. And then when were 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 were done with that its 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 were going to subscribe and well see you next time .
Thank you for watching all the articles on the topic C Programming Tutorial – 51 – How to Read Files. All shares of amritsang.org are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.