(upbeat music)
- [Instructor] Your challenge for this chapter
was to read a text file, decode it,
store its data in a structure and print the results.
And here is my solution.
This is zero eight solution,
from chapter eight of the exercise files.
You can see that this solution is a bit longer,
than some of the earlier solutions in this course.
Let's take a look at how this works,
I start by including some headers.
I've got my stdio,
my errno, stdlib and cstring.
I declare some constants,
the maximum size of a line buffer,
for reading the lines of text, from the text file.
A maximum size of name string, and description string.
I've got the path to my file name,
and you're going to need to modify this.
Obviously your home directory is not named
to billw on your system.
So you'll need to find out
what the name of that directory is and update that.
And if you're on windows,
you might want to put a C colon
or whatever drive letter your exercise files are on
at the beginning of that string.
So this path needs to be actually the path to the text file,
otherwise, you're going to get a file not found error
which will be conveniently printed out
because we're using error number in our code.
Maximum number of splits has to be the maximum number
of tabs to be found in a line in the file
and here's a tab character
And I just use a constant for that
because it makes the code a little bit more readable.
Here's the simple structure for each item.
We have the skew,
we have the item name and the item description.
This is a function
for finding all the tabs in a line of text.
And I just found this to be the easiest way to do this.
It returns an array where the first element of the array
is the number of tabs.
And then each element in array is an index
the count of number of characters into the string
where you will find that tab.
And so it's very simple to do.
We have a simple loop.
We look for a tab character.
If we find one, we increment the count.
We store the index there in our indicies array.
And if we get to the maximum number of tabs,
we end our loop.
One little trick that I do here
which is just a little space saving trick
it's just my kind of thing.
You notice that my indices array is static.
So I have to zero it out at the beginning
to make sure that it's zero
for each iteration of this function.
And you'll notice that I take the zero element
the first element of the array
and I create a reference to it, and call it count.
And so now my count variable
is actually that first element in the array.
So I don't need to copy it or anything like that.
It's just a little shortcut using references
for something like this can be convenient.
Now, down here in our main, we have a character buffer
for reading the lines of the file.
We open the file, you'll notice, I check to see
if the file was opened successfully.
And if not I print a convenient error message
using strerror.
And then I have a loop for reading the file.
And this is where the work gets done.
We use fgets to get the file line by line,
get the length of the string.
If the length is too short, I go to the next line.
I trim the new line from the end of the string
because that just makes things a little bit easier.
And then I find all the tabs str_seps.
I find all the separators
and I make sure that those numbers are reasonable.
I change each of those separators or just the first two
because those are the only two I care about.
To terminate, I change them to zeros.
And this makes it especially easy
for me to read these strings.
Now I can just copy using strncpy,
copy these strings into my structure
and then print them out.
And you'll notice I'm using atoi
which is a function for converting a string to an integer.
Now, when we build and run.
Apple likes to make sure I have permission
to use my own computer.
And here we go,
here's our output exactly as required in the exercise.
So this is a good challenge.
Exercise a lot of your knowledge from the past few chapters.
I hope you took some time to experiment
and learn from the experience.