C Programming Tutorial For Beginners

Kelly and she’s gonna work in customer service so now when I run my program instead of overriding what was in the employees text file now this is just gonna append this new employee onto the file so I’ll come up here run the program.

And let’s open this employee’s file and you’ll see now we have another entry Kelly from customer service so when we write a file we either create a new file and add content onto it or we just override the content in the file at least using this F printf function and when we append.

We’re sub pending information.

Onto the end of the file so just using those two things.

I mean you can essentially write out any you want and it’s important to note that you can write more.

Than just text file so I could write like an HTML file or I could write like a CSS file you could write really I mean any type.

Of file that you’d want and that’s basically how we can write to a file and append it to the end of a file in this tutorial I will not teach you.

Guys how to read the information from a file in C in the last tutorial we talked about writing to files so we looked at how we could create a file overwrite a file and append information on to a file in this tutorial I’m gonna show you guys how you can.

Read information from a file so down here you’ll notice that I have my file pointer setup so I just say file and the asterisks and I’d called it FP and remember this is just a pointer which you know and a pointer is just a memory address.

And it’s the memory address of this file in memory so it’s the memory address of this employees dot txt file which we created in the last tutorial and I’m using this F open function which basically opens this file and then I’m specifying a file mode we talked about the W file mode and the a file mode now.

I want to talk to you guys about the our file mode and this stands for read so when I use the our file mode I’m actually able to read information from the file and this can be pretty cool so in order to read the information in the file I’m actually going to show you guys how we can.

Read like the individual lines of the file so actually first let’s look at the file this is employees dot txt it just has you know basically a bunch of employees their names and then what.

They do and we’re gonna be able to read this file inside of our program so the first thing I want to do is create a variable and I’m gonna create this variable up here before we create the file pointer and it’s just gonna be an.

Array of characters and I’m.

Line and I’m gonna give this an initial size of.

255 so be able to store 255 characters and then down here in order to read this file I can actually read the file one line at a time and we can use a special function called F gets and F gets basically is going to allow us to read the lines of the file one by one so I could say F gets and I.

Need to give this a few different parameters the first parameter I need to give it is a place to store the information that.

It gets so this function is going to go and it’s going to read the information from the file and it’s going to store it somewhere so the first parameter we need to give this function is where it should store that information so in our case we’re going to store it inside of that align variable that we created up there the second thing we need to give this is a size and this is basically just gonna be like.

The maximum size that we can read in so I’m just gonna say 255 basically this number should match this number up here and then we also want to give it that file pointer so I’m just gonna say f pointer and basically what this is gonna do is it’s going to read the first line of the file and it’s going to store it inside of this line variable so if I was to come down here and.

Say print F and print it out a string and just print out line now when I print out a line it’s actually going to be have the value of the first line in the file so let’s run this program and you’ll see.

Printing out Jim salesman and if you didn’t remember that was the first line inside of that employees dot text so.

Basically what F gets is doing is it’s reading the first line and another thing.

That it’s doing is it’s moving this pointer so remember this pointer is essentially pointing to where the file is in our memory and whenever we use F gets this pointer is gonna get incremented so it’s basically gonna move past the first line in the file so if I was to use.

This again for example if I said f gets again when I use this it’s gonna.

Store the value of the first line of the file inside of line and when I use it again it’s now gonna grab the second line in the file so if I print.

This out now this should actually be storing the second line in the file so when I run my program you’ll see it’s storing the second line Pam receptionist I can basically do this for all of the lines in the file so that’s basically how we can use F gets to read the individual lines inside of a file.