Hello, and welcome back.
We have our first challenge for doing some file writing and reading.
Specifically, in this challenge, we're just going to read in some
characters, get you familiar with the fputc function.
So what I would like you to do is I would like you to write
a program to find the total number of lines in a text file.
So you need to create a file that contains some lines of text.
You're going to have to create a file.
Remember, this file has to be placed in either where the
executable is and then you can double click the executable and run
it in the same directory as that.
Or if you're running it from the integrated development environment,
you're running it from code blocks then you can put your text file
inside of wherever your source files are located wherever you saved your
project inside your c source file.
So you'll have to create a file before you can actually write this
program because you need to read and write some data inside that file.
You're going to need to open your test file up.
We're going to need the f open call this is going to get
you familiar with the basics.
You need to use the fgetc function to parse characters in
a file that you get until you hit the eof, the end of file.
So if it's end of file, you can increment the counter before you reach
the end of file, that's how you're going to be able to count the lines.
So you can just create a local variable for the counter.
And what you want to do display is you want to display as output the total
number of lines in the file itself.
So this is going to be a basic program that gets you familiar with
the basics of file input and output.
Specifically, always want to open up a file using fopen.
We're going to get familiar with that file pointer.
We're going to have to close our file and then we're going to use
that important fputc function.
So your while loop, you're going to have to create a local
variable that's just a counter.
And as you're reading in each character, you can compare the
character to the line feed.
That's returned.
And if you have a line feed then you want to increment the counter
so that you count each line.
Whole purpose of this function is to count the number of lines in a file.
So good luck.
We'll be using the basics here.
And I hope this gets you familiar with using a file pointer
and opening a file in fgetc.
Thank you.