OK.
Welcome back to another lecture in this lecture, we are going to be talking about command line arguments,
but we will also be kind of doing some more stuff with the input stream's input file stream not working
with the output file stream in this lecture, but we will be practicing more with the input file stream.
And we're also going to be, of course, introducing this new topic, Gramling.
So we want to make our IO programs more flexible at this point.
So currently, the only thing that we can really do with our input and output from files involves us
hard coding the name of the file in our program, right?
So we have that dot open and then we put like my file dot, right?
So think about this.
What if you compiled that program and you turn it into an executable and then you sent it to your friend
and you're like losing this cool program?
It reads, it reads a file, and then it like, does some stuff with that information and it prints
some cool stuff out.
And your friend is like, Oh, cool, let me run it.
I'm going to make a file.
And I call it like, super cool file data and I'm going to run the program.
It's like, Well, your program is not going to be able to open that file because you told it to open
my file that text.
What if you just wanted to be able to send it to your friend and have them put whatever input file that
they want?
You know, once it's an executable, they can't really change your code.
It's already turned into a binary file, right?
So.
It's kind of annoying you would basically he'd be like, Oh, it doesn't run.
I need to.
How do I get it to run with this simple file?
And you're like, Oh, well, let me make another version that has, you know, it opens super cool
filed or whatever.
That would be kind of annoying.
You'd have to go back and change your code.
So how would we make this work without having to hard code the actual name inside the open function,
the name of the file?
So this is where these main function parameters and arguments come in.
These are also referred to as command line arguments.
You're probably seeing this and being like, Whoa, what is going on?
The main function can have parameters.
That's super weird.
Where would the parameters come from if Maine is the first function to run?
So isn't it like the operating system calling Maine and then all the rest of the program happens starting
in Maine?
So where are the arguments being sent from?
Well, yes, it's kind of coming from the operating system, but really it can be coming from you and
from you.
I mean, by coming from the command line, the command prompt the console, whatever you want to call
that bash whatever environment you're in.
So let's go ahead and take a closer look at that.
So what would that look like?
Well, here I'm in bash.
So what I would do if I had something like this is I would run my program.
So we've obviously seen something like this.
But then I put a space and I put something else.
And what is this other thing?
Well, it's my input file.
I put an input file that text and what I am doing is I'm sending this all this information.
To the main function right here, and this stuff right here is being used as parameters right here.
So, OK, let's look into the parameters more what the these mean, we see an end ARG C and a cha pointer
ARG V, which looks like an array, so ARG C stands for argument count and argument count is literally
just one thing to thing like this.
It's how many things we see here.
There's a space separating two things.
The first thing, though, happens to be our actual program that's getting rand.
That's kind of weird, right?
The second thing kind of makes sense because it's the input file we're interested in.
But that's how it works.
It takes both of those into consideration.
The Cha star are the reasons for argument, Victor, we haven't introduced the concept of a vector yet,
but right now you can just imagine a vector as another name for an array.
So you can really think of this as argument array, and it looks like an array, right?
Because it has these brackets here.
So that's what it is.
It's an array of what type of character you write, and it's a pointer so you can put Char Star Ruby
in here or you can do char star star.
So you can do two stars, one star.
I'm just doing one right now.
You can kind of look that up.
If you'd like to know more about that convention and why you can have one and two stars, you know,
a pointer that stores some pointers or just one tar star.
So you can look that stuff up if you'd like to know more about that.
But I'm just saying, you know, you can do either or I think it kind of depends on the compiler a little
bit, but we're going to use our star.
So RV is just an argument vector or an argument array.
So let's break down this example a little bit more, so let's look at this bash line that I had right
here where we're running my program, that CP and we are passing the main function.
This information, which includes input file that, he added.
In this example, artsy equals two.
So this is one and this is two.
So there's two things here.
Total on this line.
RV looks like this.
It is an array, in fact, with all of this information.
But you notice that we have my program, not CP, as the first element and input file that as the second
element of the RV array.
So.
This my program does CP can be referred to as argue zero, right, we can index the array at position
zero, which is the first thing.
And so if we say argue zero, it should return to us this my program to CP.
Similarly, we can do argue be one to get input file that because this is position one, right, we
know that.
We know that because we've done arrays already and we know about position starting at zero and then
one and then two and then three and so on, right?
So two things in here position zero, position one to get them out of the array, you would do arguing
zero with the subscript operator to index it and argue, be one with the subscript operator and indexing
it with one.
So pretty strange that includes all this, including the program that you're actually running, right,
which will be our whole program, but that's just the way it is.
So now you kind of see how we can use this stuff.
I'm going to increment or say, I'm going to show you one extra little thing before we go over a challenge.
So this is looping with input.
So I kind of mentioned this in the last lecture.
I talked about us potentially going over looping with input files and the input file streams, and that
is what I'm showing you right here.
So you have made while loops and you have seen that we can repeatedly ask for input inside of the while
loop, right?
Like somewhere down here in the indentation within the brackets.
But I don't have brackets here.
You can also do something like this, so here we have an integer, we have an input file stream that
we created.
We open a file connected to the stream.
But what's going on here?
I'm basically putting this in a condition of the while loop.
I'm using the stream and the input operator with a variable in here.
This is basically saying loop, while you are able to get stuff from the stream out of the file as input
and read it into my integer, so as long as this is possible.
Then keep looping.
And this is kind of a standard convention for reading from files, if you want to read files where you
don't know how big they are, which is going to be a common thing, I mean, it's not normal to necessarily
know how big the file is.
You want to write a program that just kind of accepts whatever file size and just reads it.
So this lets you loop through a file of some unknown length, and it will keep reading from the stream
into this integer as long as that's possible until it reaches the end of the file, right when it reaches
the end of the file.
It'll kind of automatically stop because it won't be able to read into my integer anymore, and this
is just a nice C++ convention.
There's some interesting stuff going on with reading from files, there's actually like an end of file
kind of Boolean flag that you can check.
That's another useful thing.
We might look at that a little bit more in the future, but for right now, I just want to introduce
this concept so you can solve the next challenge that I'm about to show you on the next slide.
So just remember that you can loop over a file with an input stream like this.
I also want to show that it is possible to do something like this, except we haven't really done that
yet.
And of course, you know, this will just keep going as long as you're entering input.
So you might want to add some sort of other condition here or something like that.
But I do want to say that this is not something that is special just for a stream.
You don't have to have some input file stream to be able to do something like this in a while loop.
This is technically possible.
Of course, you would want to think about when you're going to stop taking input from the console.
Right?
Just want to point that out?
Put that out there.
So let's go ahead and start with a challenge.
So now that you know, how hard can we work?
And if you already forgot, you can go back to the video and kind of look at what we've gone over.
But what you should do is make a program that uses command line arguments.
So you're going to use RC and RV stuff to read a file of integers into eight dynamic array.
So you'll be using a new keyword, making a pointer, getting a dynamic array going and then the first
line of the file that you read in from.
That's going to be an integer that tells you how many numbers are in the file.
So it also does not include that integer itself.
So.
For example, if we have if you make a file, which you should make for this problem, you should make
your own text file with integers.
At the top of that file, and if you have the integer 12.
That means that below that, 12 in the file on the next lines, the subsequent lines, you're going
to have 12 different lines where each line has a number that you will read into the array.
And that 12 is not including the 12 itself, it's the counting starts on the next line below it.
All right.
So the first line says 12.
The next line below it is where you start counting and that 12 tells you that there's going to be 12
lines after that first line.
OK.
So once you have read in all of those lines into your array, then you should print out the contents
of the array.
Also remember, since it's a dynamic array, you should handle deleting that memory so it is now freed
up at the end of your program.
Also, remember to close the input file stream.
So those are just two things that I want to remind you about.
So you should make some file like input, text or whatever you want to name it.
But that file should contain at least 10 numbers in it, not including the first line that tells you
how many numbers are in the file and also make sure that you're not hard coding this file name.
You should be getting it from the command line arguments like we just discussed and accessing it with
the the right.
The argument vector, so I'm going to go ahead and let you get into this, if you're still a little
confused on how to make a text file, you can basically make a new file in vs code and you can just,
of course, make one and then just make a dot extension file and open that.
And then.
Same thing, you can save it as text with this code if you are on Sea Lion, which don't worry, we
will be transitioning to Sea Lion very soon.
I will be using Sea Lion, I'll be using all sorts of editors and mixing and matching, you know, but
I'm going to be using Sea Lion pretty soon here exclusively because we're going to be making bigger
programs.
I will show you how to make a.
Set up in sea line for this problem, like use the command line arguments because it's a little it's
not as straightforward.
It would be good for you to be able to google that stuff on your own and figure it out.
You just need to edit the configurations, but I will go ahead and show you that.
So first, I will go ahead and show how to do that sea lion and then I will go over the solution to
this small challenge, but make sure to attempt it yourself before looking at the solution.
As always, do not try and look up answers to this.
You can look up syntax stuff if you are forgetting some syntax, but make sure to solve the problem
on your own.
If you cannot solve it on your own, then you don't have to worry.
You can wait to the solution, but please try your hardest to solve it on your own.
So let me go ahead and open sea lion first.
I want to go ahead and.
Make a new project from sources, I kind of just have this directory here.
Right here we have the files that are all just kind of smashed into the first program folder.
So remember, I'm just kind of storing it messily here.
You should definitely organize it better than I am.
You should not smash it all into this folder.
I think I've already mentioned that, but you should have your own folders and organize all of these.
Programs that we've been going over so far, so I have this seemed arcs right here.
It has this zip file, this kind of blinking here, and I'm going to solve it in VSCO.
But first, I'm going to show you how to set it up with Sea Lion, so I'm going to go ahead and use
this.
So this right here, I just press, OK, I make a new sea make project.
It's just going to have these project files here.
So I'm going to say, OK.
This should be.
Yeah.
I won't do that.
All right.
So now what we're going to do, so we have this file right here, right?
So let me increase this size a little bit.
So you can see what's going on here, but this is just some basic file.
It has some command line argument here.
I've already put that here and then I don't have anything else in it and I will solve the problem in
a moment I'm going to do in vs code.
But what you need to do to be able to pass some files as input to your program, since you may not be
using a command line down here like in the terminal.
So here's there's like a terminal down here, right?
So I'm assuming in here.
We have this terminal down here, but maybe you're not necessarily going to be running this program
down here like the CMG are, it's sleepy and then put input file that takes you like that.
You can do that in the terminal.
But what I want to show you is if you want to just run it directly from sea lion.
You can actually go up here and go to edit configurations.
And here in program arguments, you would put your input once input, okay, so let's say you have a
file in here in your directory, so I'm going to say cancel right now.
Let's see you want to make a new file.
So I right click here, I say new and I just say file and I say input.
Now I have some input.
I say, like, you know, I'm going to put 12 things here, and then I just put like, you know, a
bunch of random things until there's like.
Well, you know, there should be 12 here, one two three four five six seven eight nine, 10, 12,
13.
OK, so I guess there's.
13.
Yeah, so there should be, you know, 12 things in here, you make some file like this.
Maybe, you know, I go ahead and I just save everything and then I go back here and I'm like, Well,
how do I use this?
As if I didn't want to use the terminal so I can go here.
Any configurations go here.
Input text.
It's going to be program arguments.
You can think of this as arguments to main.
So it's going to send this whatever you type in here as the arguments for main, just like you would
do in the terminal down there, like we've discussed.
You also might want to go here and click on this and find your actual theme the ARGS Directory, your
project directory and then click Apply and OK.
And after that, you will be able to run it normally here, and you would be able to see your input
text file would actually get used here in the arguments to me.
OK, so I just wanted to point that out.
So you're able to use that.
So I'm actually going to exit out of here.
I'm not going to save that.
So let's go ahead and head over to vs code so we can so we can deal with that.
So I'm actually going to open the code here.
So I've got this seemed ARG's copy here.
I'm going to go ahead and navigate to this directory, so I'm going to say first program and CMT Arts.
So now I am here.
Let's make sure that we can actually.
See stuff big enough.
OK, so let's go ahead and get started with these solutions, so what do we have to do here?
So we're going to need to go ahead and go back to our problem statement.
So we want to make a program that uses a command line arguments to read a file of integers into a dynamic
array.
Let's just get started with that knowledge right here.
So something that I'm going to do is I'm going to make sure that the user is in fact entering into arguments
that our count should be to write Argosy should be to.
So I'm going to say if our fee is not equal to two, I want to print some message.
Some of this you control and see, I want to say usage to show how they use it.
And I'm going to say, like, you know, programs, something like this program name.
And then let's just say input file names, something like that.
So that should be the way that you should use it.
So this is where the input file names should go, and then if they if they fail to do that correctly,
I'm honestly just going to exit immediately with this negative one exit code because that is something
bad.
They should not have tried to run it without an input file name because it would cause problems.
So I'm going to go ahead and do that.
And then down here, if it if that is all good to go, I'm going to go ahead and create an input file
stream.
So I'll just say extreme and call this my stream.
So now we have an input file stream.
What I'm then going to do is I'm going to see.
I also have an integer here, so I'm going to say and I will say in size because I'm going to want to
read in that size right.
The first line should be the size of all the stuff in the file, but it's also going to be the size
for our dynamic array.
After that, I'm going to have the value, which is basically each one of the numbers that we're going
to read in.
And after that, I'm going to have another and that is just going to be position or count or something
like that.
You call it position count.
And that's actually going to start at zero because I'm going to use a while loop and I'm going to want
to increment the position so I can use that to index the array right because we want some sort of position
that we can use while we're looping, just like you've done before, to put stuff in the ordered positions
in the array.
I also should probably go ahead and just declare my array.
But I think what I'm going to do is I need to read in from the stream first to get that size right.
So what I will do is I will say my stream, my stream that open.
What am I going to put here now?
Am I going to put input, text or something?
No, I'm not going to.
I have this file right here numbs that text that has 12 numbers.
So I think one, two, three four five, six, seven eight.
Nine, 10, 11, 12, 13 numbers, so let me tell you, I made the same mistake as last time.
So go ahead and save that and I'm not going to put non-stop text in here because that would be hard
coded and that defeats the purpose.
That's not what we're doing.
What am I going to put in here?
Well, I'm going to use our movie because we're going to pass on his command line arguments and what
position was it in if we have program name as the zero position?
This is going to be the first position.
We have two things.
The first thing, the second thing, but this is position zero, this position one.
So I'm interested in R V and the argument vector or the argument a position one that's going to give
me the name of the input file right here is going to take that and put it right here.
And it's going to be a string, just like as if we put, you know, NUM's Dot.
So let's go ahead and do that.
And once we open that, what is the first thing we want to do?
Well, we want to read in that no reading from the input stream right into this size because we know
that that's going to be the first thing.
And once we have that size, then what we can do is say something like this and let's just say and poyner,
my array equals new and and we're going to save this dynamic array is going to be size.
It's going to have the size sighs we've read into the size already, OK?
So what do we do now?
Well, now we want to go through the rest of the items in the array and save them, so the rest of the
items in the file and save them into the array.
So it says, read a file of integers into a dynamic array.
We've handled this first line.
That's an integer.
It states how many numbers are in the file we've already put.
That is the size of the array.
So we want to read it into the array and then print it out, so let's go ahead and do that.
I must say while we are able to have my extreme excuse me read into and what are we going to read into
what variable unmarried into value?
Because that's going to hold each number that we're reading from the file.
So what it'll be like is, you know, first time the loop goes around, it'll be for next time, it
will be eight.
Next time be thirty four and so on.
Some might say while we can go, most read from my stream and store that data into the value variable.
Then what I'm going to do down here is I'm going to say position count is already holding the value
zero.
I started at zero, right?
So I'm going to use my ram, the same my array and I'm going to say position, position, count, right?
So we're going to index it a position count.
It starts at zero.
So the first time we go through the loop, it's going to store position zero right of the array and
we're we've already read in the size.
Which is 12.
So the next number we're going to read in is going to be four.
Right?
So I'm going to say that I'm going to store value each time, right?
And if I want to store value each time and have it be for.
And then the next thing in there is eight, and the next thing is 34, I need to change the position,
I need to increment it, so I'm actually going to do a position count plus plus and that'll change my
position.
So this is enough to successfully read into every position in the array.
So we've already got the size of the file.
That's assuming, of course, that this is correct and there actually are 12 things in the file.
So I'm actually going to remove this line right here as well and save that just because I don't need
that line.
So let's go ahead and deal with that now we can print out all the stuff in the array, right?
So I'm going to use a for loop for this.
So at this point, I can close my stream because I don't need to read anything anymore.
So I'm going to say my stream got close, then I'm going to do four and I equals zero.
I is less than size I plus plus because we're going to loop as long as there are things in the array
and the array has size number of things, right?
And then now what I'm going to do is, say, a steady out and I'm going to put my array.
I and then I'm just going to put a little space in between.
Then down here, before we return zero, we should delete the array, right, so I'm going to say delete
because we dynamically allocated it, so we're going to put these brackets, so delete everything in
the array and I'm going to save my arrays so it'll delete that whole contiguous chunk of memory.
We've talked about this syntax before, right?
So now we are successfully freeing up that memory of our dynamically allocated array since we use the
keyword new.
We've closed our stream, we're reading and everything OK.
And it seems like we have something to catch if the user does not enter the correct number of arguments.
So we should be good to go.
I'm going to go ahead and save this.
And then I am going to go ahead and compile it, source CGI plus plus you just can't make sure that
it's actually here.
So if I say cat, the args that CP, so I'm in here.
Let's see if we have.
So we all have this CMake stuff that got put in here as well.
So this is actually stored in first program, so I should move that I'm actually going to.
This was in our CMake program, so I'm actually in the wrong direction.
I'm going to go back one directory because our thing that's actually here, if we can't see in the hour
is one copy.
It has all this stuff in it.
It's not the, you know, this shows all of our code.
This is what we're dealing with right now.
We're not dealing with the one that I had in the CIMIC.
Sorry, the.
Sea lion directory that we had.
So let's go ahead and compile this, let's say, seemed parks one that CBP dash Oh, and I'm just going
to say CMT Arts Dot XY.
So that compiles, OK, let's go ahead and run it and see if it works, OK?
So let's do you know CMA, CMT Arts?
Yes.
And let's check it out.
Oh, well, look at that.
I tried to run it and it didn't work, it said that I need to do usage program name, input, file name.
So you notice how that worked at exited right away, and it printed out this message now that informs
me, Oh, I can't just run it like this, I need to run it with, of course, you know, I've used this
bash syntax here.
You would use whatever you need to use for whatever shell or console you're in.
But I need to use an input file name, so let's go ahead and do this again, and let's use the NUM's
Dot.
Now I'm to.
And look at that.
Let's take a look at these numbers here.
We have four, eight four.
Let's just match it up with this.
So four eight three four one 13 five 26.
Four to seven.
54, one, 20, seven, nine, two thousand seven hundred forty nine, so it printed out and read all
those in and successfully printed them all out in a line.
So if you weren't able to get that like I always say, don't worry, you know, we keep introducing
is pretty new, pretty difficult projects, sorry concepts.
And you know, I don't expect you to immediately just understand everything right off the bat.
So if you don't understand it, go ahead and walk back through it.
Look through all the code.
You know, the stuff that we did here was just to kind of make sure that the user knows how to use the
program correctly with an input file name.
Right here, we declare all of our variables, you know, this is for the size of the array that we're
going to dynamically allocate, which is the 12 appears the first line.
This is to hold each one of the values.
We're going to keep changing what it holds, which will be first four, then eight point thirty four
each time our loop goes right.
So we did that value position count is just so we can index our array at the correct position.
And since we're looping, we do a plus plus to bring it from zero to one to two to three.
And then we store four at position zero, a position one, three, four, position two and so on by
doing this right here.
We've opened the stream, we read the initial size in before we started, then reading into the values
from the stream.
From that file, we closed the stream before we printed it out and then we made sure to delete our dam
dynamically allocated.
OK.
So with that, I will see you in the next lecture.
We are going to go over something that is a little bit easier to use now than an array.
It's going to introduce a really cool new concept, and we're going to actually talk about that vector
that I was mentioning when I said our argument vector.
We're going to talk about a new data type, which is actually going to be a new container called a vector.
And it's like an array, but it's much fancier and easier for you to use.
So with that, I see you in the next lecture.