Hello, and welcome back.
In this lecture, I want to provide a demonstration of our first
challenge for reading from a file.
We're going to count the number of lines inside of a file by using fgetc.
So let's bring up code blocks, and let's create our project.
Here, we're just going to say new project, and it's going
to be a basic reading align.
So say console application select c, and we'll name this basic
or counting number of lines how about count number of lines.
And so this is going to create our main.c which is good.
Before we even start this application though, we want to actually create
our file that has some text in it.
Since we're not writing, we're just reading in the number of lines.
So you have to know where you saved your project.
I'm saving my projects inside of my downloads directory.
And when you create a new project inside your downloads directory,
you go to that project name.
My project name is count number of lines.
So inside count number of lines, I have my main.c file.
I can create my file right in this directory.
So I'm just going to right click inside the directory and
I'm going to say new and I'm going to create a text file.
This is a new text document, and it's going to be test.text.
I'm going to open it up.
You can open up in any editor, this is wordpad.
It's going to put in some lines, hello.
This will be testing for accounting, okay, a, just a
bunch of lines and then you want to save that and close it.
Now that I have my file created, I can actually start implementing something.
The first thing I want to do is I want to create a constant
variable for the file name.
So I'm just going to use define.
This going to represent my file name.
I'm just going to say file name in all caps, and this is going to be equal
to whatever I just named it, which again is test.text, so test.text.
And all my code is going to be my main function.
I want to create my file pointer.
Remember, to create a file pointer, you just use all caps FILE, and then
say star and then you can just say fp.
And I'm going to initialize that to null.
Whenever you do file reading and writing, you have to make
sure you include standard lib.h.
I'm also going to create a single character that's going to be used.
I'm just going to say char, and then I'm going to create
a counter for all my lines.
I'm going to say int lines count.
And I'm going to initialize that to 0.
First thing you need to do when you do any kind of reading from
files, you have to open it up.
So the f open function is going to return a file pointer.
So I want to assign that to my fp variable and I want to say
f open and the name of the file name which I already defined
is a constant, file name.
And I want to open up for reading and reading only so I just say r.
So here I want to do fopen with my file name as my constant and then
r in double quotes that's our mode.
I want to only read only.
I do not want to write.
If I did an r plus, I would write as well.
Before we use the pointer, the pointer represents the file.
I want to make sure that the pointer is not equal to
null, so I want to do an f.
I want to say fp is equal to null.
Then if it's equal to null, I just want to display an error in return.
So I want to do something like printf file does not exist,
whatever you want to do.
And this is going to then.
We're just going to want to return a negative 1 from our function.
After we've opened the file, now we can actually start reading characters,
so we can write a while loop and we want to say while our character,
which is a character equals fgetc.
Fgetc is reading a single character.
We pass in our file pointer, and we want to basically say we want to
read characters until we see an eof.
So we say until it's not equal to eof.
Then we're going to jump out of our loop, something like this.
So we're going to read in characters until we get to the end of the file.
As long as our character is not a line feed, we want to only
increment when we see a line feed.
So we're going to say if our character is equal to a line feed, then we want
to basically increment our counter.
We just want to say lines count plus plus, that's going to count our lines.
And then we jump out of the loop when we get to the end of the file.
But we only recommend our account if we get a line feed.
The last thing you always want to do after you've done reading your writing
is you want to close your file.
So you want to say fclose and then pass in the file pointer.
And then we'd also want to set our file pointer equal to null.
And then we can print out the number of lines that we run in.
So we can just say printf total number of lines r percent d backslash n and
then we can just put lines count.
And then just return from the main, just like that.
So again to go over this code, we create our file pointer set it to
null create a character and a local variable for counting the lines.
We open up our file in line 12.
We open it up in mode for read only.
We make sure it's not null, and then we do our while loop to
read in each character until we're at the end of the file.
Every time we see a line feed, we increment that counter.
And then we close our file and set our pointer to null.
And then we print out the number of lines.
Let's go ahead and build this.
And we do have an extra line feed, we want to make sure.
So I want to make sure we don't forget the comma in our printf.
And now we run this, it's going to read from our file that we created
and tell us how many lines we have.
So it says total number of lines are six.
If we open up our text file, we should notice that there are six lines,
one, two, three, four, five, six.
There's seven actually.
So we'd have to increment the last one until we get the eof.
So here's a little bug here where we just say because this
is doing our last line feed before we hit the end of file.
So you'd want to actually have this be one more and then we would
run it and it would actually work.
So in this case we'll add a couple lines.
And in this case, we should have one, two, three, four, five,
six, seven, eight, nine lines.
We close the file.
We run it again, compile it, and we should get nine.
In this case we get 9.
So I'll save this source code.
You can download it.
This is the basics of reading data from a file using fgetc.
You also want to become familiar with fgets.
And then to format output, you can use fscanf to read in formatted output.
I hope this was helpful. Thank you.