1
00:00:01,070 --> 00:00:01,400
OK.

2
00:00:01,430 --> 00:00:06,590
Welcome back to another lecture in this lecture, we are going to be talking about command line arguments,

3
00:00:06,590 --> 00:00:15,170
but we will also be kind of doing some more stuff with the input stream's input file stream not working

4
00:00:15,170 --> 00:00:20,240
with the output file stream in this lecture, but we will be practicing more with the input file stream.

5
00:00:20,240 --> 00:00:24,350
And we're also going to be, of course, introducing this new topic, Gramling.

6
00:00:25,880 --> 00:00:30,860
So we want to make our IO programs more flexible at this point.

7
00:00:30,860 --> 00:00:38,060
So currently, the only thing that we can really do with our input and output from files involves us

8
00:00:38,060 --> 00:00:41,210
hard coding the name of the file in our program, right?

9
00:00:41,210 --> 00:00:45,290
So we have that dot open and then we put like my file dot, right?

10
00:00:46,100 --> 00:00:47,210
So think about this.

11
00:00:47,210 --> 00:00:53,810
What if you compiled that program and you turn it into an executable and then you sent it to your friend

12
00:00:53,810 --> 00:00:55,250
and you're like losing this cool program?

13
00:00:55,250 --> 00:01:04,610
It reads, it reads a file, and then it like, does some stuff with that information and it prints

14
00:01:04,610 --> 00:01:05,480
some cool stuff out.

15
00:01:05,480 --> 00:01:08,120
And your friend is like, Oh, cool, let me run it.

16
00:01:08,510 --> 00:01:09,470
I'm going to make a file.

17
00:01:09,480 --> 00:01:15,140
And I call it like, super cool file data and I'm going to run the program.

18
00:01:15,150 --> 00:01:19,670
It's like, Well, your program is not going to be able to open that file because you told it to open

19
00:01:19,670 --> 00:01:20,870
my file that text.

20
00:01:22,110 --> 00:01:27,360
What if you just wanted to be able to send it to your friend and have them put whatever input file that

21
00:01:27,360 --> 00:01:28,020
they want?

22
00:01:28,050 --> 00:01:31,290
You know, once it's an executable, they can't really change your code.

23
00:01:31,290 --> 00:01:34,580
It's already turned into a binary file, right?

24
00:01:34,590 --> 00:01:35,160
So.

25
00:01:36,270 --> 00:01:39,960
It's kind of annoying you would basically he'd be like, Oh, it doesn't run.

26
00:01:40,620 --> 00:01:41,640
I need to.

27
00:01:41,760 --> 00:01:43,360
How do I get it to run with this simple file?

28
00:01:43,360 --> 00:01:49,170
And you're like, Oh, well, let me make another version that has, you know, it opens super cool

29
00:01:49,170 --> 00:01:50,910
filed or whatever.

30
00:01:50,910 --> 00:01:52,340
That would be kind of annoying.

31
00:01:52,350 --> 00:01:53,760
You'd have to go back and change your code.

32
00:01:54,090 --> 00:02:02,430
So how would we make this work without having to hard code the actual name inside the open function,

33
00:02:02,440 --> 00:02:03,330
the name of the file?

34
00:02:04,950 --> 00:02:09,580
So this is where these main function parameters and arguments come in.

35
00:02:09,600 --> 00:02:13,110
These are also referred to as command line arguments.

36
00:02:13,650 --> 00:02:17,130
You're probably seeing this and being like, Whoa, what is going on?

37
00:02:17,160 --> 00:02:19,410
The main function can have parameters.

38
00:02:19,530 --> 00:02:20,880
That's super weird.

39
00:02:21,690 --> 00:02:25,800
Where would the parameters come from if Maine is the first function to run?

40
00:02:25,830 --> 00:02:32,100
So isn't it like the operating system calling Maine and then all the rest of the program happens starting

41
00:02:32,100 --> 00:02:32,610
in Maine?

42
00:02:33,300 --> 00:02:35,580
So where are the arguments being sent from?

43
00:02:35,610 --> 00:02:43,260
Well, yes, it's kind of coming from the operating system, but really it can be coming from you and

44
00:02:43,260 --> 00:02:43,950
from you.

45
00:02:43,960 --> 00:02:50,340
I mean, by coming from the command line, the command prompt the console, whatever you want to call

46
00:02:50,340 --> 00:02:53,220
that bash whatever environment you're in.

47
00:02:54,810 --> 00:02:57,390
So let's go ahead and take a closer look at that.

48
00:02:57,810 --> 00:02:59,280
So what would that look like?

49
00:02:59,310 --> 00:03:00,600
Well, here I'm in bash.

50
00:03:01,830 --> 00:03:06,740
So what I would do if I had something like this is I would run my program.

51
00:03:06,750 --> 00:03:08,610
So we've obviously seen something like this.

52
00:03:08,610 --> 00:03:11,320
But then I put a space and I put something else.

53
00:03:11,790 --> 00:03:13,140
And what is this other thing?

54
00:03:13,770 --> 00:03:15,420
Well, it's my input file.

55
00:03:15,450 --> 00:03:22,350
I put an input file that text and what I am doing is I'm sending this all this information.

56
00:03:23,290 --> 00:03:28,810
To the main function right here, and this stuff right here is being used as parameters right here.

57
00:03:31,540 --> 00:03:38,260
So, OK, let's look into the parameters more what the these mean, we see an end ARG C and a cha pointer

58
00:03:38,380 --> 00:03:47,410
ARG V, which looks like an array, so ARG C stands for argument count and argument count is literally

59
00:03:47,410 --> 00:03:50,890
just one thing to thing like this.

60
00:03:50,890 --> 00:03:52,660
It's how many things we see here.

61
00:03:52,660 --> 00:03:54,820
There's a space separating two things.

62
00:03:55,510 --> 00:03:59,830
The first thing, though, happens to be our actual program that's getting rand.

63
00:03:59,830 --> 00:04:01,270
That's kind of weird, right?

64
00:04:01,720 --> 00:04:05,320
The second thing kind of makes sense because it's the input file we're interested in.

65
00:04:05,950 --> 00:04:07,120
But that's how it works.

66
00:04:07,330 --> 00:04:09,970
It takes both of those into consideration.

67
00:04:11,230 --> 00:04:19,810
The Cha star are the reasons for argument, Victor, we haven't introduced the concept of a vector yet,

68
00:04:20,230 --> 00:04:24,850
but right now you can just imagine a vector as another name for an array.

69
00:04:25,330 --> 00:04:29,290
So you can really think of this as argument array, and it looks like an array, right?

70
00:04:29,290 --> 00:04:30,520
Because it has these brackets here.

71
00:04:31,510 --> 00:04:32,470
So that's what it is.

72
00:04:32,830 --> 00:04:41,220
It's an array of what type of character you write, and it's a pointer so you can put Char Star Ruby

73
00:04:41,230 --> 00:04:43,930
in here or you can do char star star.

74
00:04:45,190 --> 00:04:47,370
So you can do two stars, one star.

75
00:04:47,380 --> 00:04:48,700
I'm just doing one right now.

76
00:04:49,600 --> 00:04:51,070
You can kind of look that up.

77
00:04:51,070 --> 00:04:55,360
If you'd like to know more about that convention and why you can have one and two stars, you know,

78
00:04:55,360 --> 00:05:00,610
a pointer that stores some pointers or just one tar star.

79
00:05:01,060 --> 00:05:03,670
So you can look that stuff up if you'd like to know more about that.

80
00:05:03,670 --> 00:05:10,450
But I'm just saying, you know, you can do either or I think it kind of depends on the compiler a little

81
00:05:10,450 --> 00:05:11,980
bit, but we're going to use our star.

82
00:05:14,380 --> 00:05:17,980
So RV is just an argument vector or an argument array.

83
00:05:18,670 --> 00:05:25,090
So let's break down this example a little bit more, so let's look at this bash line that I had right

84
00:05:25,090 --> 00:05:30,150
here where we're running my program, that CP and we are passing the main function.

85
00:05:30,430 --> 00:05:33,970
This information, which includes input file that, he added.

86
00:05:34,810 --> 00:05:36,880
In this example, artsy equals two.

87
00:05:36,910 --> 00:05:39,420
So this is one and this is two.

88
00:05:39,430 --> 00:05:41,020
So there's two things here.

89
00:05:41,050 --> 00:05:42,370
Total on this line.

90
00:05:43,420 --> 00:05:45,550
RV looks like this.

91
00:05:45,560 --> 00:05:48,670
It is an array, in fact, with all of this information.

92
00:05:48,880 --> 00:05:55,420
But you notice that we have my program, not CP, as the first element and input file that as the second

93
00:05:55,420 --> 00:05:57,490
element of the RV array.

94
00:05:58,570 --> 00:05:59,050
So.

95
00:06:00,830 --> 00:06:07,010
This my program does CP can be referred to as argue zero, right, we can index the array at position

96
00:06:07,020 --> 00:06:08,840
zero, which is the first thing.

97
00:06:09,650 --> 00:06:14,150
And so if we say argue zero, it should return to us this my program to CP.

98
00:06:14,570 --> 00:06:20,870
Similarly, we can do argue be one to get input file that because this is position one, right, we

99
00:06:20,870 --> 00:06:21,440
know that.

100
00:06:21,460 --> 00:06:26,360
We know that because we've done arrays already and we know about position starting at zero and then

101
00:06:26,360 --> 00:06:28,880
one and then two and then three and so on, right?

102
00:06:29,600 --> 00:06:35,300
So two things in here position zero, position one to get them out of the array, you would do arguing

103
00:06:35,300 --> 00:06:43,070
zero with the subscript operator to index it and argue, be one with the subscript operator and indexing

104
00:06:43,070 --> 00:06:43,610
it with one.

105
00:06:45,760 --> 00:06:51,610
So pretty strange that includes all this, including the program that you're actually running, right,

106
00:06:51,610 --> 00:06:54,130
which will be our whole program, but that's just the way it is.

107
00:06:55,660 --> 00:06:59,450
So now you kind of see how we can use this stuff.

108
00:06:59,470 --> 00:07:07,960
I'm going to increment or say, I'm going to show you one extra little thing before we go over a challenge.

109
00:07:09,610 --> 00:07:11,860
So this is looping with input.

110
00:07:11,860 --> 00:07:15,340
So I kind of mentioned this in the last lecture.

111
00:07:15,340 --> 00:07:23,590
I talked about us potentially going over looping with input files and the input file streams, and that

112
00:07:23,590 --> 00:07:24,910
is what I'm showing you right here.

113
00:07:25,240 --> 00:07:32,980
So you have made while loops and you have seen that we can repeatedly ask for input inside of the while

114
00:07:32,980 --> 00:07:33,430
loop, right?

115
00:07:33,430 --> 00:07:36,970
Like somewhere down here in the indentation within the brackets.

116
00:07:36,970 --> 00:07:38,230
But I don't have brackets here.

117
00:07:39,270 --> 00:07:44,910
You can also do something like this, so here we have an integer, we have an input file stream that

118
00:07:44,910 --> 00:07:45,570
we created.

119
00:07:45,780 --> 00:07:48,090
We open a file connected to the stream.

120
00:07:48,810 --> 00:07:50,040
But what's going on here?

121
00:07:50,070 --> 00:07:54,750
I'm basically putting this in a condition of the while loop.

122
00:07:54,750 --> 00:07:59,220
I'm using the stream and the input operator with a variable in here.

123
00:08:00,110 --> 00:08:10,310
This is basically saying loop, while you are able to get stuff from the stream out of the file as input

124
00:08:10,310 --> 00:08:13,910
and read it into my integer, so as long as this is possible.

125
00:08:15,310 --> 00:08:16,810
Then keep looping.

126
00:08:18,180 --> 00:08:24,840
And this is kind of a standard convention for reading from files, if you want to read files where you

127
00:08:24,840 --> 00:08:31,380
don't know how big they are, which is going to be a common thing, I mean, it's not normal to necessarily

128
00:08:31,380 --> 00:08:32,870
know how big the file is.

129
00:08:32,880 --> 00:08:38,190
You want to write a program that just kind of accepts whatever file size and just reads it.

130
00:08:39,540 --> 00:08:46,490
So this lets you loop through a file of some unknown length, and it will keep reading from the stream

131
00:08:46,560 --> 00:08:55,110
into this integer as long as that's possible until it reaches the end of the file, right when it reaches

132
00:08:55,110 --> 00:08:56,670
the end of the file.

133
00:08:57,690 --> 00:09:04,950
It'll kind of automatically stop because it won't be able to read into my integer anymore, and this

134
00:09:04,950 --> 00:09:07,260
is just a nice C++ convention.

135
00:09:08,350 --> 00:09:15,760
There's some interesting stuff going on with reading from files, there's actually like an end of file

136
00:09:16,120 --> 00:09:18,730
kind of Boolean flag that you can check.

137
00:09:19,270 --> 00:09:20,950
That's another useful thing.

138
00:09:21,460 --> 00:09:27,340
We might look at that a little bit more in the future, but for right now, I just want to introduce

139
00:09:27,340 --> 00:09:32,950
this concept so you can solve the next challenge that I'm about to show you on the next slide.

140
00:09:34,090 --> 00:09:38,980
So just remember that you can loop over a file with an input stream like this.

141
00:09:39,880 --> 00:09:47,050
I also want to show that it is possible to do something like this, except we haven't really done that

142
00:09:47,050 --> 00:09:47,380
yet.

143
00:09:48,520 --> 00:09:53,500
And of course, you know, this will just keep going as long as you're entering input.

144
00:09:53,500 --> 00:09:58,840
So you might want to add some sort of other condition here or something like that.

145
00:09:58,850 --> 00:10:04,060
But I do want to say that this is not something that is special just for a stream.

146
00:10:04,600 --> 00:10:10,760
You don't have to have some input file stream to be able to do something like this in a while loop.

147
00:10:10,780 --> 00:10:12,670
This is technically possible.

148
00:10:12,940 --> 00:10:18,820
Of course, you would want to think about when you're going to stop taking input from the console.

149
00:10:18,940 --> 00:10:19,300
Right?

150
00:10:19,990 --> 00:10:21,910
Just want to point that out?

151
00:10:21,910 --> 00:10:22,660
Put that out there.

152
00:10:22,990 --> 00:10:25,060
So let's go ahead and start with a challenge.

153
00:10:26,230 --> 00:10:28,420
So now that you know, how hard can we work?

154
00:10:28,420 --> 00:10:33,010
And if you already forgot, you can go back to the video and kind of look at what we've gone over.

155
00:10:34,270 --> 00:10:38,470
But what you should do is make a program that uses command line arguments.

156
00:10:38,860 --> 00:10:45,640
So you're going to use RC and RV stuff to read a file of integers into eight dynamic array.

157
00:10:45,640 --> 00:10:52,000
So you'll be using a new keyword, making a pointer, getting a dynamic array going and then the first

158
00:10:52,000 --> 00:10:53,980
line of the file that you read in from.

159
00:10:53,980 --> 00:10:57,100
That's going to be an integer that tells you how many numbers are in the file.

160
00:10:57,100 --> 00:10:59,680
So it also does not include that integer itself.

161
00:10:59,680 --> 00:11:00,010
So.

162
00:11:01,070 --> 00:11:05,780
For example, if we have if you make a file, which you should make for this problem, you should make

163
00:11:05,780 --> 00:11:07,520
your own text file with integers.

164
00:11:08,650 --> 00:11:12,370
At the top of that file, and if you have the integer 12.

165
00:11:13,470 --> 00:11:20,160
That means that below that, 12 in the file on the next lines, the subsequent lines, you're going

166
00:11:20,160 --> 00:11:28,230
to have 12 different lines where each line has a number that you will read into the array.

167
00:11:29,290 --> 00:11:37,480
And that 12 is not including the 12 itself, it's the counting starts on the next line below it.

168
00:11:37,750 --> 00:11:38,070
All right.

169
00:11:38,080 --> 00:11:40,870
So the first line says 12.

170
00:11:41,440 --> 00:11:47,620
The next line below it is where you start counting and that 12 tells you that there's going to be 12

171
00:11:47,770 --> 00:11:51,220
lines after that first line.

172
00:11:51,790 --> 00:11:52,240
OK.

173
00:11:53,350 --> 00:12:02,560
So once you have read in all of those lines into your array, then you should print out the contents

174
00:12:02,560 --> 00:12:03,550
of the array.

175
00:12:03,580 --> 00:12:08,920
Also remember, since it's a dynamic array, you should handle deleting that memory so it is now freed

176
00:12:08,920 --> 00:12:10,990
up at the end of your program.

177
00:12:11,410 --> 00:12:13,930
Also, remember to close the input file stream.

178
00:12:13,930 --> 00:12:17,860
So those are just two things that I want to remind you about.

179
00:12:18,610 --> 00:12:23,470
So you should make some file like input, text or whatever you want to name it.

180
00:12:24,990 --> 00:12:32,910
But that file should contain at least 10 numbers in it, not including the first line that tells you

181
00:12:33,390 --> 00:12:40,440
how many numbers are in the file and also make sure that you're not hard coding this file name.

182
00:12:40,440 --> 00:12:46,380
You should be getting it from the command line arguments like we just discussed and accessing it with

183
00:12:46,380 --> 00:12:47,580
the the right.

184
00:12:48,840 --> 00:12:56,250
The argument vector, so I'm going to go ahead and let you get into this, if you're still a little

185
00:12:56,250 --> 00:13:03,060
confused on how to make a text file, you can basically make a new file in vs code and you can just,

186
00:13:03,060 --> 00:13:08,340
of course, make one and then just make a dot extension file and open that.

187
00:13:08,340 --> 00:13:08,880
And then.

188
00:13:10,260 --> 00:13:19,170
Same thing, you can save it as text with this code if you are on Sea Lion, which don't worry, we

189
00:13:19,170 --> 00:13:22,080
will be transitioning to Sea Lion very soon.

190
00:13:22,470 --> 00:13:27,990
I will be using Sea Lion, I'll be using all sorts of editors and mixing and matching, you know, but

191
00:13:27,990 --> 00:13:33,720
I'm going to be using Sea Lion pretty soon here exclusively because we're going to be making bigger

192
00:13:33,720 --> 00:13:34,260
programs.

193
00:13:34,890 --> 00:13:37,860
I will show you how to make a.

194
00:13:39,780 --> 00:13:44,880
Set up in sea line for this problem, like use the command line arguments because it's a little it's

195
00:13:44,880 --> 00:13:46,050
not as straightforward.

196
00:13:46,270 --> 00:13:50,160
It would be good for you to be able to google that stuff on your own and figure it out.

197
00:13:51,000 --> 00:13:54,600
You just need to edit the configurations, but I will go ahead and show you that.

198
00:13:56,000 --> 00:14:01,730
So first, I will go ahead and show how to do that sea lion and then I will go over the solution to

199
00:14:01,730 --> 00:14:07,910
this small challenge, but make sure to attempt it yourself before looking at the solution.

200
00:14:07,910 --> 00:14:11,370
As always, do not try and look up answers to this.

201
00:14:11,390 --> 00:14:16,760
You can look up syntax stuff if you are forgetting some syntax, but make sure to solve the problem

202
00:14:16,760 --> 00:14:17,270
on your own.

203
00:14:18,140 --> 00:14:21,950
If you cannot solve it on your own, then you don't have to worry.

204
00:14:21,950 --> 00:14:24,980
You can wait to the solution, but please try your hardest to solve it on your own.

205
00:14:26,150 --> 00:14:28,700
So let me go ahead and open sea lion first.

206
00:14:32,090 --> 00:14:33,590
I want to go ahead and.

207
00:14:34,730 --> 00:14:40,140
Make a new project from sources, I kind of just have this directory here.

208
00:14:41,960 --> 00:14:51,830
Right here we have the files that are all just kind of smashed into the first program folder.

209
00:14:51,890 --> 00:14:54,800
So remember, I'm just kind of storing it messily here.

210
00:14:55,370 --> 00:14:57,560
You should definitely organize it better than I am.

211
00:14:57,560 --> 00:14:59,690
You should not smash it all into this folder.

212
00:14:59,700 --> 00:15:03,800
I think I've already mentioned that, but you should have your own folders and organize all of these.

213
00:15:05,050 --> 00:15:09,430
Programs that we've been going over so far, so I have this seemed arcs right here.

214
00:15:09,730 --> 00:15:14,380
It has this zip file, this kind of blinking here, and I'm going to solve it in VSCO.

215
00:15:14,380 --> 00:15:22,150
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

216
00:15:22,150 --> 00:15:22,480
this.

217
00:15:22,930 --> 00:15:29,140
So this right here, I just press, OK, I make a new sea make project.

218
00:15:29,140 --> 00:15:32,440
It's just going to have these project files here.

219
00:15:33,430 --> 00:15:34,750
So I'm going to say, OK.

220
00:15:38,740 --> 00:15:39,730
This should be.

221
00:15:41,660 --> 00:15:42,030
Yeah.

222
00:15:48,290 --> 00:15:48,980
I won't do that.

223
00:15:51,040 --> 00:15:51,430
All right.

224
00:15:51,580 --> 00:15:55,510
So now what we're going to do, so we have this file right here, right?

225
00:15:58,970 --> 00:16:01,670
So let me increase this size a little bit.

226
00:16:06,180 --> 00:16:10,650
So you can see what's going on here, but this is just some basic file.

227
00:16:10,670 --> 00:16:12,530
It has some command line argument here.

228
00:16:12,530 --> 00:16:16,850
I've already put that here and then I don't have anything else in it and I will solve the problem in

229
00:16:16,850 --> 00:16:18,170
a moment I'm going to do in vs code.

230
00:16:18,860 --> 00:16:27,140
But what you need to do to be able to pass some files as input to your program, since you may not be

231
00:16:27,140 --> 00:16:30,530
using a command line down here like in the terminal.

232
00:16:30,740 --> 00:16:33,170
So here's there's like a terminal down here, right?

233
00:16:34,010 --> 00:16:36,260
So I'm assuming in here.

234
00:16:37,740 --> 00:16:47,940
We have this terminal down here, but maybe you're not necessarily going to be running this program

235
00:16:47,940 --> 00:16:56,460
down here like the CMG are, it's sleepy and then put input file that takes you like that.

236
00:16:56,970 --> 00:16:58,170
You can do that in the terminal.

237
00:16:58,180 --> 00:17:02,460
But what I want to show you is if you want to just run it directly from sea lion.

238
00:17:04,320 --> 00:17:08,550
You can actually go up here and go to edit configurations.

239
00:17:09,880 --> 00:17:17,620
And here in program arguments, you would put your input once input, okay, so let's say you have a

240
00:17:17,620 --> 00:17:21,700
file in here in your directory, so I'm going to say cancel right now.

241
00:17:22,060 --> 00:17:23,590
Let's see you want to make a new file.

242
00:17:23,590 --> 00:17:28,900
So I right click here, I say new and I just say file and I say input.

243
00:17:30,070 --> 00:17:31,160
Now I have some input.

244
00:17:32,350 --> 00:17:39,460
I say, like, you know, I'm going to put 12 things here, and then I just put like, you know, a

245
00:17:39,460 --> 00:17:45,010
bunch of random things until there's like.

246
00:17:46,230 --> 00:17:51,690
Well, you know, there should be 12 here, one two three four five six seven eight nine, 10, 12,

247
00:17:51,690 --> 00:17:52,030
13.

248
00:17:52,050 --> 00:17:53,130
OK, so I guess there's.

249
00:17:57,110 --> 00:17:57,430
13.

250
00:17:58,010 --> 00:18:05,270
Yeah, so there should be, you know, 12 things in here, you make some file like this.

251
00:18:05,270 --> 00:18:10,310
Maybe, you know, I go ahead and I just save everything and then I go back here and I'm like, Well,

252
00:18:10,310 --> 00:18:11,100
how do I use this?

253
00:18:11,100 --> 00:18:13,700
As if I didn't want to use the terminal so I can go here.

254
00:18:13,700 --> 00:18:15,390
Any configurations go here.

255
00:18:15,870 --> 00:18:17,010
Input text.

256
00:18:17,600 --> 00:18:19,560
It's going to be program arguments.

257
00:18:19,580 --> 00:18:21,920
You can think of this as arguments to main.

258
00:18:22,310 --> 00:18:28,130
So it's going to send this whatever you type in here as the arguments for main, just like you would

259
00:18:28,130 --> 00:18:30,470
do in the terminal down there, like we've discussed.

260
00:18:30,920 --> 00:18:37,070
You also might want to go here and click on this and find your actual theme the ARGS Directory, your

261
00:18:37,070 --> 00:18:42,170
project directory and then click Apply and OK.

262
00:18:42,920 --> 00:18:50,510
And after that, you will be able to run it normally here, and you would be able to see your input

263
00:18:50,630 --> 00:18:55,250
text file would actually get used here in the arguments to me.

264
00:18:56,300 --> 00:18:58,340
OK, so I just wanted to point that out.

265
00:18:58,340 --> 00:19:00,830
So you're able to use that.

266
00:19:01,790 --> 00:19:03,240
So I'm actually going to exit out of here.

267
00:19:03,260 --> 00:19:04,310
I'm not going to save that.

268
00:19:05,450 --> 00:19:14,200
So let's go ahead and head over to vs code so we can so we can deal with that.

269
00:19:14,210 --> 00:19:17,540
So I'm actually going to open the code here.

270
00:19:21,890 --> 00:19:25,280
So I've got this seemed ARG's copy here.

271
00:19:25,580 --> 00:19:34,630
I'm going to go ahead and navigate to this directory, so I'm going to say first program and CMT Arts.

272
00:19:35,450 --> 00:19:36,920
So now I am here.

273
00:19:39,150 --> 00:19:41,070
Let's make sure that we can actually.

274
00:19:43,440 --> 00:19:44,910
See stuff big enough.

275
00:19:49,170 --> 00:19:54,960
OK, so let's go ahead and get started with these solutions, so what do we have to do here?

276
00:19:55,170 --> 00:19:58,410
So we're going to need to go ahead and go back to our problem statement.

277
00:19:59,570 --> 00:20:05,300
So we want to make a program that uses a command line arguments to read a file of integers into a dynamic

278
00:20:05,300 --> 00:20:05,600
array.

279
00:20:05,630 --> 00:20:07,460
Let's just get started with that knowledge right here.

280
00:20:07,470 --> 00:20:14,660
So something that I'm going to do is I'm going to make sure that the user is in fact entering into arguments

281
00:20:14,660 --> 00:20:17,610
that our count should be to write Argosy should be to.

282
00:20:17,630 --> 00:20:27,530
So I'm going to say if our fee is not equal to two, I want to print some message.

283
00:20:28,730 --> 00:20:34,160
Some of this you control and see, I want to say usage to show how they use it.

284
00:20:34,160 --> 00:20:44,030
And I'm going to say, like, you know, programs, something like this program name.

285
00:20:44,030 --> 00:20:51,230
And then let's just say input file names, something like that.

286
00:20:53,040 --> 00:20:55,290
So that should be the way that you should use it.

287
00:20:59,860 --> 00:21:03,940
So this is where the input file names should go, and then if they if they fail to do that correctly,

288
00:21:03,940 --> 00:21:10,990
I'm honestly just going to exit immediately with this negative one exit code because that is something

289
00:21:10,990 --> 00:21:11,230
bad.

290
00:21:11,230 --> 00:21:16,420
They should not have tried to run it without an input file name because it would cause problems.

291
00:21:17,620 --> 00:21:19,330
So I'm going to go ahead and do that.

292
00:21:19,330 --> 00:21:25,030
And then down here, if it if that is all good to go, I'm going to go ahead and create an input file

293
00:21:25,030 --> 00:21:25,420
stream.

294
00:21:25,420 --> 00:21:30,310
So I'll just say extreme and call this my stream.

295
00:21:31,210 --> 00:21:33,070
So now we have an input file stream.

296
00:21:33,700 --> 00:21:37,570
What I'm then going to do is I'm going to see.

297
00:21:37,570 --> 00:21:45,370
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

298
00:21:45,370 --> 00:21:47,060
read in that size right.

299
00:21:47,080 --> 00:21:52,960
The first line should be the size of all the stuff in the file, but it's also going to be the size

300
00:21:52,960 --> 00:21:54,220
for our dynamic array.

301
00:21:55,120 --> 00:22:00,700
After that, I'm going to have the value, which is basically each one of the numbers that we're going

302
00:22:00,700 --> 00:22:01,480
to read in.

303
00:22:02,020 --> 00:22:08,080
And after that, I'm going to have another and that is just going to be position or count or something

304
00:22:08,080 --> 00:22:08,590
like that.

305
00:22:08,740 --> 00:22:10,090
You call it position count.

306
00:22:11,440 --> 00:22:15,580
And that's actually going to start at zero because I'm going to use a while loop and I'm going to want

307
00:22:15,580 --> 00:22:23,260
to increment the position so I can use that to index the array right because we want some sort of position

308
00:22:23,260 --> 00:22:28,900
that we can use while we're looping, just like you've done before, to put stuff in the ordered positions

309
00:22:28,900 --> 00:22:29,560
in the array.

310
00:22:31,380 --> 00:22:34,260
I also should probably go ahead and just declare my array.

311
00:22:35,910 --> 00:22:40,740
But I think what I'm going to do is I need to read in from the stream first to get that size right.

312
00:22:41,460 --> 00:22:50,940
So what I will do is I will say my stream, my stream that open.

313
00:22:51,720 --> 00:22:53,000
What am I going to put here now?

314
00:22:53,010 --> 00:22:55,530
Am I going to put input, text or something?

315
00:22:55,530 --> 00:22:57,200
No, I'm not going to.

316
00:22:57,420 --> 00:23:03,840
I have this file right here numbs that text that has 12 numbers.

317
00:23:06,810 --> 00:23:10,830
So I think one, two, three four five, six, seven eight.

318
00:23:13,040 --> 00:23:17,270
Nine, 10, 11, 12, 13 numbers, so let me tell you, I made the same mistake as last time.

319
00:23:17,550 --> 00:23:24,290
So go ahead and save that and I'm not going to put non-stop text in here because that would be hard

320
00:23:24,290 --> 00:23:25,570
coded and that defeats the purpose.

321
00:23:25,580 --> 00:23:26,630
That's not what we're doing.

322
00:23:27,560 --> 00:23:28,850
What am I going to put in here?

323
00:23:28,970 --> 00:23:35,150
Well, I'm going to use our movie because we're going to pass on his command line arguments and what

324
00:23:35,150 --> 00:23:39,890
position was it in if we have program name as the zero position?

325
00:23:40,520 --> 00:23:42,800
This is going to be the first position.

326
00:23:42,800 --> 00:23:43,820
We have two things.

327
00:23:43,820 --> 00:23:47,570
The first thing, the second thing, but this is position zero, this position one.

328
00:23:48,830 --> 00:23:56,420
So I'm interested in R V and the argument vector or the argument a position one that's going to give

329
00:23:56,420 --> 00:24:01,340
me the name of the input file right here is going to take that and put it right here.

330
00:24:01,340 --> 00:24:07,030
And it's going to be a string, just like as if we put, you know, NUM's Dot.

331
00:24:10,730 --> 00:24:12,830
So let's go ahead and do that.

332
00:24:12,830 --> 00:24:15,440
And once we open that, what is the first thing we want to do?

333
00:24:15,470 --> 00:24:25,250
Well, we want to read in that no reading from the input stream right into this size because we know

334
00:24:25,250 --> 00:24:26,750
that that's going to be the first thing.

335
00:24:27,050 --> 00:24:37,400
And once we have that size, then what we can do is say something like this and let's just say and poyner,

336
00:24:37,400 --> 00:24:46,640
my array equals new and and we're going to save this dynamic array is going to be size.

337
00:24:48,070 --> 00:24:51,220
It's going to have the size sighs we've read into the size already, OK?

338
00:24:52,420 --> 00:24:54,310
So what do we do now?

339
00:24:54,370 --> 00:25:00,310
Well, now we want to go through the rest of the items in the array and save them, so the rest of the

340
00:25:00,310 --> 00:25:02,270
items in the file and save them into the array.

341
00:25:02,290 --> 00:25:07,420
So it says, read a file of integers into a dynamic array.

342
00:25:07,420 --> 00:25:09,220
We've handled this first line.

343
00:25:09,220 --> 00:25:10,180
That's an integer.

344
00:25:10,450 --> 00:25:12,640
It states how many numbers are in the file we've already put.

345
00:25:12,640 --> 00:25:14,160
That is the size of the array.

346
00:25:15,300 --> 00:25:19,200
So we want to read it into the array and then print it out, so let's go ahead and do that.

347
00:25:20,110 --> 00:25:29,370
I must say while we are able to have my extreme excuse me read into and what are we going to read into

348
00:25:29,370 --> 00:25:32,010
what variable unmarried into value?

349
00:25:32,010 --> 00:25:37,200
Because that's going to hold each number that we're reading from the file.

350
00:25:37,890 --> 00:25:42,450
So what it'll be like is, you know, first time the loop goes around, it'll be for next time, it

351
00:25:42,450 --> 00:25:43,000
will be eight.

352
00:25:43,020 --> 00:25:44,880
Next time be thirty four and so on.

353
00:25:45,750 --> 00:25:52,650
Some might say while we can go, most read from my stream and store that data into the value variable.

354
00:25:55,520 --> 00:26:02,320
Then what I'm going to do down here is I'm going to say position count is already holding the value

355
00:26:02,330 --> 00:26:02,870
zero.

356
00:26:02,870 --> 00:26:04,180
I started at zero, right?

357
00:26:04,190 --> 00:26:12,940
So I'm going to use my ram, the same my array and I'm going to say position, position, count, right?

358
00:26:12,950 --> 00:26:14,690
So we're going to index it a position count.

359
00:26:14,690 --> 00:26:15,760
It starts at zero.

360
00:26:15,770 --> 00:26:23,330
So the first time we go through the loop, it's going to store position zero right of the array and

361
00:26:23,330 --> 00:26:25,370
we're we've already read in the size.

362
00:26:26,620 --> 00:26:27,700
Which is 12.

363
00:26:27,760 --> 00:26:30,880
So the next number we're going to read in is going to be four.

364
00:26:31,000 --> 00:26:31,360
Right?

365
00:26:32,670 --> 00:26:37,380
So I'm going to say that I'm going to store value each time, right?

366
00:26:38,070 --> 00:26:41,460
And if I want to store value each time and have it be for.

367
00:26:42,600 --> 00:26:46,380
And then the next thing in there is eight, and the next thing is 34, I need to change the position,

368
00:26:46,440 --> 00:26:54,630
I need to increment it, so I'm actually going to do a position count plus plus and that'll change my

369
00:26:54,630 --> 00:26:55,140
position.

370
00:26:56,720 --> 00:27:01,520
So this is enough to successfully read into every position in the array.

371
00:27:02,390 --> 00:27:05,180
So we've already got the size of the file.

372
00:27:05,900 --> 00:27:11,630
That's assuming, of course, that this is correct and there actually are 12 things in the file.

373
00:27:12,920 --> 00:27:17,780
So I'm actually going to remove this line right here as well and save that just because I don't need

374
00:27:17,780 --> 00:27:18,260
that line.

375
00:27:20,440 --> 00:27:27,280
So let's go ahead and deal with that now we can print out all the stuff in the array, right?

376
00:27:27,460 --> 00:27:29,200
So I'm going to use a for loop for this.

377
00:27:30,100 --> 00:27:33,640
So at this point, I can close my stream because I don't need to read anything anymore.

378
00:27:33,640 --> 00:27:41,080
So I'm going to say my stream got close, then I'm going to do four and I equals zero.

379
00:27:41,530 --> 00:27:48,160
I is less than size I plus plus because we're going to loop as long as there are things in the array

380
00:27:48,160 --> 00:27:51,010
and the array has size number of things, right?

381
00:27:52,030 --> 00:27:59,030
And then now what I'm going to do is, say, a steady out and I'm going to put my array.

382
00:28:00,470 --> 00:28:04,720
I and then I'm just going to put a little space in between.

383
00:28:06,490 --> 00:28:12,250
Then down here, before we return zero, we should delete the array, right, so I'm going to say delete

384
00:28:12,430 --> 00:28:17,230
because we dynamically allocated it, so we're going to put these brackets, so delete everything in

385
00:28:17,230 --> 00:28:21,430
the array and I'm going to save my arrays so it'll delete that whole contiguous chunk of memory.

386
00:28:21,730 --> 00:28:23,740
We've talked about this syntax before, right?

387
00:28:24,460 --> 00:28:29,950
So now we are successfully freeing up that memory of our dynamically allocated array since we use the

388
00:28:29,950 --> 00:28:30,670
keyword new.

389
00:28:31,030 --> 00:28:34,660
We've closed our stream, we're reading and everything OK.

390
00:28:35,110 --> 00:28:40,150
And it seems like we have something to catch if the user does not enter the correct number of arguments.

391
00:28:41,050 --> 00:28:42,400
So we should be good to go.

392
00:28:42,430 --> 00:28:44,350
I'm going to go ahead and save this.

393
00:28:46,290 --> 00:28:52,110
And then I am going to go ahead and compile it, source CGI plus plus you just can't make sure that

394
00:28:52,110 --> 00:28:52,940
it's actually here.

395
00:28:52,950 --> 00:29:01,560
So if I say cat, the args that CP, so I'm in here.

396
00:29:04,090 --> 00:29:06,220
Let's see if we have.

397
00:29:07,850 --> 00:29:12,020
So we all have this CMake stuff that got put in here as well.

398
00:29:15,190 --> 00:29:20,230
So this is actually stored in first program, so I should move that I'm actually going to.

399
00:29:23,970 --> 00:29:26,830
This was in our CMake program, so I'm actually in the wrong direction.

400
00:29:26,850 --> 00:29:33,900
I'm going to go back one directory because our thing that's actually here, if we can't see in the hour

401
00:29:33,900 --> 00:29:35,460
is one copy.

402
00:29:36,420 --> 00:29:38,250
It has all this stuff in it.

403
00:29:38,730 --> 00:29:41,880
It's not the, you know, this shows all of our code.

404
00:29:42,870 --> 00:29:44,220
This is what we're dealing with right now.

405
00:29:44,230 --> 00:29:48,360
We're not dealing with the one that I had in the CIMIC.

406
00:29:48,780 --> 00:29:49,500
Sorry, the.

407
00:29:50,730 --> 00:29:52,380
Sea lion directory that we had.

408
00:29:53,460 --> 00:30:02,280
So let's go ahead and compile this, let's say, seemed parks one that CBP dash Oh, and I'm just going

409
00:30:02,280 --> 00:30:06,690
to say CMT Arts Dot XY.

410
00:30:08,410 --> 00:30:11,830
So that compiles, OK, let's go ahead and run it and see if it works, OK?

411
00:30:12,280 --> 00:30:15,750
So let's do you know CMA, CMT Arts?

412
00:30:16,300 --> 00:30:16,480
Yes.

413
00:30:17,650 --> 00:30:18,880
And let's check it out.

414
00:30:20,030 --> 00:30:21,750
Oh, well, look at that.

415
00:30:21,770 --> 00:30:27,710
I tried to run it and it didn't work, it said that I need to do usage program name, input, file name.

416
00:30:29,510 --> 00:30:34,340
So you notice how that worked at exited right away, and it printed out this message now that informs

417
00:30:34,340 --> 00:30:40,220
me, Oh, I can't just run it like this, I need to run it with, of course, you know, I've used this

418
00:30:40,220 --> 00:30:41,420
bash syntax here.

419
00:30:41,420 --> 00:30:45,800
You would use whatever you need to use for whatever shell or console you're in.

420
00:30:47,690 --> 00:30:51,590
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

421
00:30:51,590 --> 00:30:52,130
Dot.

422
00:30:53,060 --> 00:30:53,850
Now I'm to.

423
00:30:56,760 --> 00:30:57,840
And look at that.

424
00:30:57,870 --> 00:30:59,400
Let's take a look at these numbers here.

425
00:30:59,410 --> 00:31:01,010
We have four, eight four.

426
00:31:01,890 --> 00:31:03,340
Let's just match it up with this.

427
00:31:03,340 --> 00:31:09,000
So four eight three four one 13 five 26.

428
00:31:10,520 --> 00:31:11,750
Four to seven.

429
00:31:13,120 --> 00:31:18,760
54, one, 20, seven, nine, two thousand seven hundred forty nine, so it printed out and read all

430
00:31:18,760 --> 00:31:22,450
those in and successfully printed them all out in a line.

431
00:31:24,130 --> 00:31:30,190
So if you weren't able to get that like I always say, don't worry, you know, we keep introducing

432
00:31:30,190 --> 00:31:34,750
is pretty new, pretty difficult projects, sorry concepts.

433
00:31:35,290 --> 00:31:41,020
And you know, I don't expect you to immediately just understand everything right off the bat.

434
00:31:41,470 --> 00:31:45,570
So if you don't understand it, go ahead and walk back through it.

435
00:31:45,580 --> 00:31:46,690
Look through all the code.

436
00:31:47,470 --> 00:31:55,300
You know, the stuff that we did here was just to kind of make sure that the user knows how to use the

437
00:31:55,300 --> 00:31:57,760
program correctly with an input file name.

438
00:32:00,030 --> 00:32:05,160
Right here, we declare all of our variables, you know, this is for the size of the array that we're

439
00:32:05,160 --> 00:32:11,160
going to dynamically allocate, which is the 12 appears the first line.

440
00:32:11,250 --> 00:32:14,100
This is to hold each one of the values.

441
00:32:14,100 --> 00:32:19,380
We're going to keep changing what it holds, which will be first four, then eight point thirty four

442
00:32:19,380 --> 00:32:20,840
each time our loop goes right.

443
00:32:20,850 --> 00:32:26,760
So we did that value position count is just so we can index our array at the correct position.

444
00:32:26,760 --> 00:32:31,950
And since we're looping, we do a plus plus to bring it from zero to one to two to three.

445
00:32:31,950 --> 00:32:40,500
And then we store four at position zero, a position one, three, four, position two and so on by

446
00:32:40,500 --> 00:32:41,550
doing this right here.

447
00:32:43,330 --> 00:32:49,210
We've opened the stream, we read the initial size in before we started, then reading into the values

448
00:32:49,210 --> 00:32:49,960
from the stream.

449
00:32:50,050 --> 00:32:55,450
From that file, we closed the stream before we printed it out and then we made sure to delete our dam

450
00:32:55,640 --> 00:32:56,600
dynamically allocated.

451
00:32:58,300 --> 00:32:58,810
OK.

452
00:32:58,840 --> 00:33:02,120
So with that, I will see you in the next lecture.

453
00:33:02,150 --> 00:33:09,120
We are going to go over something that is a little bit easier to use now than an array.

454
00:33:09,130 --> 00:33:13,900
It's going to introduce a really cool new concept, and we're going to actually talk about that vector

455
00:33:14,680 --> 00:33:17,640
that I was mentioning when I said our argument vector.

456
00:33:17,650 --> 00:33:24,190
We're going to talk about a new data type, which is actually going to be a new container called a vector.

457
00:33:24,190 --> 00:33:28,360
And it's like an array, but it's much fancier and easier for you to use.

458
00:33:29,080 --> 00:33:30,820
So with that, I see you in the next lecture.
