1
00:00:00,360 --> 00:00:03,660
Everybody, welcome back to another lecture.

2
00:00:03,870 --> 00:00:10,170
So in this lecture, we are going to be talking about files, dreams, files, dreams are a way to read

3
00:00:10,170 --> 00:00:12,570
and write data to files.

4
00:00:12,570 --> 00:00:17,910
So to and from really so we can read data from files and write data to files.

5
00:00:17,910 --> 00:00:23,520
So we're doing something similar that we've to what we've done so far with the console, right?

6
00:00:23,520 --> 00:00:30,090
We've been able to read data from the console with CNN, and we've been able to write data to the console

7
00:00:30,090 --> 00:00:30,870
with see out.

8
00:00:31,260 --> 00:00:33,840
Now we're going to talk about how to use files.

9
00:00:35,100 --> 00:00:36,750
So we're kind of getting more real.

10
00:00:36,750 --> 00:00:42,120
And by that, I mean, reading from files is a lot more realistic than having a program that only takes

11
00:00:42,120 --> 00:00:43,230
input from the console.

12
00:00:44,160 --> 00:00:49,470
So we're about to learn how to not only take input right and read from files, but we're going to learn

13
00:00:49,470 --> 00:00:51,030
how to write from them as well.

14
00:00:52,260 --> 00:00:56,940
So that requires us to include something called f stream.

15
00:00:57,810 --> 00:01:05,310
So it's similar to the Io stream, right, which is input output stream, but instead this is used for

16
00:01:05,310 --> 00:01:07,830
files instead of reading writing to console.

17
00:01:09,090 --> 00:01:12,420
So this time the stream is going to be flowing to and from a file.

18
00:01:12,780 --> 00:01:19,500
So the f here stands for file straight right, f for file and then stream similar to input output stream.

19
00:01:21,030 --> 00:01:23,450
The first thing we're going to talk about is reading from a file.

20
00:01:23,460 --> 00:01:27,660
So this is if stream or you can think of it as input file stream.

21
00:01:28,860 --> 00:01:35,940
So to do this, what we're going to need to do is create a variable of type extreme stream, which is

22
00:01:35,940 --> 00:01:36,990
the input file stream, right?

23
00:01:37,740 --> 00:01:42,840
So it's similar to like if we had some integer my aunt right, we say it would be like integers, the

24
00:01:42,840 --> 00:01:44,760
data type and my end is the variable.

25
00:01:44,790 --> 00:01:48,840
This is extreme as the data type and my stream as the variable name.

26
00:01:52,920 --> 00:01:57,690
So the next thing that we need to do is connect the stream to the file that we want to read by using

27
00:01:57,690 --> 00:02:00,420
a special function that can open that file.

28
00:02:00,990 --> 00:02:07,590
You notice right here we have our variable right, which is the stream that we created, that we declared

29
00:02:08,160 --> 00:02:09,190
my stream.

30
00:02:09,390 --> 00:02:12,740
And then we use the dot open function, right?

31
00:02:12,750 --> 00:02:14,760
So this is a member function as a dot.

32
00:02:14,970 --> 00:02:20,670
So this is kind of similar to when we had a string variable and did dot length.

33
00:02:21,590 --> 00:02:28,040
But now what we're doing is recalling the open function on the mice dream input, file stream, variable

34
00:02:28,040 --> 00:02:36,530
rate, my stream dot open is going to enable us to open the file that's inside the parentheses here

35
00:02:36,530 --> 00:02:37,730
as an argument, right?

36
00:02:37,760 --> 00:02:40,280
And notice that this is a string as a double quotes.

37
00:02:40,280 --> 00:02:47,240
So the open function requires and expects a string to be in here, right?

38
00:02:47,720 --> 00:02:56,300
So we're going to put double quotes and that is going to be the file that we want to read data in from.

39
00:02:56,310 --> 00:03:00,200
So this case and this example, I'm saying my file, that text.

40
00:03:00,560 --> 00:03:00,890
Right?

41
00:03:01,660 --> 00:03:08,120
That is what we are going to open to be able to read data in from, and we're connecting it to our stream

42
00:03:08,120 --> 00:03:08,390
here.

43
00:03:10,660 --> 00:03:15,400
So at this point in time, we are ready to read into a variable, just like we did with console input,

44
00:03:15,400 --> 00:03:16,510
which was seen right.

45
00:03:17,440 --> 00:03:19,870
So here I declare a variable called my aunt.

46
00:03:20,170 --> 00:03:29,380
And here, similar to how you see in, I have this was what this is basically where CNN used to be right

47
00:03:29,380 --> 00:03:32,060
when we were dealing with C and it was on this side of this.

48
00:03:32,080 --> 00:03:38,800
Operator And then our variable is on this side and the data kind of flow was flowing from the console

49
00:03:38,800 --> 00:03:40,420
and into our variable.

50
00:03:40,690 --> 00:03:41,500
Same thing here.

51
00:03:41,500 --> 00:03:44,860
The data is flowing from the input stream.

52
00:03:44,860 --> 00:03:46,690
This is our input file stream, right?

53
00:03:47,660 --> 00:03:53,360
It flows from there into our integer that we declared, so this is like, see, and except we made a

54
00:03:53,360 --> 00:03:56,180
variable of type I.F. stream, right?

55
00:03:56,840 --> 00:04:02,600
So just imagine this is a stream, but it's coming from the file that we opened and now it's flowing

56
00:04:02,600 --> 00:04:05,570
into our variable and gets stored in our variable.

57
00:04:05,960 --> 00:04:08,450
So we're going to head to the editor and make a small example program.

58
00:04:10,040 --> 00:04:16,100
So here you notice I have a stream right here, so I've included that already.

59
00:04:16,700 --> 00:04:22,070
So what I'm going to do is go ahead and make a little integer variable, and I'm just going to call

60
00:04:22,070 --> 00:04:26,540
this my end and then I'm going to do seed Conklin Extreme.

61
00:04:27,110 --> 00:04:33,240
I'm doing the second call in because I am not doing a using name space eight.

62
00:04:33,800 --> 00:04:37,250
So if you are, you don't need to do this part right here.

63
00:04:37,880 --> 00:04:40,760
You also are going to notice that I have a few more suggestions here.

64
00:04:40,760 --> 00:04:43,900
So if I say I have a stream, it's like giving me a lot of options.

65
00:04:43,910 --> 00:04:46,580
That's because I actually just recently.

66
00:04:48,630 --> 00:04:57,540
Downloaded the little add on that I had, so, you know, I had like a little C++ extension or something

67
00:04:57,540 --> 00:05:01,400
that I added on, so it's giving more C++ recommendations and stuff.

68
00:05:01,410 --> 00:05:03,510
You don't have to do that, but you could if you'd like.

69
00:05:04,920 --> 00:05:06,320
So just wanted to point that out as well.

70
00:05:06,330 --> 00:05:11,410
So I make an extreme, I'm going to call this my stream and then what do we do next?

71
00:05:11,430 --> 00:05:12,720
We're going to open it right?

72
00:05:12,900 --> 00:05:21,450
So I do my stream dot open and I'm going to actually put something called input file tucked in here.

73
00:05:22,360 --> 00:05:23,310
Why am I doing that?

74
00:05:23,340 --> 00:05:29,580
Well, let's see what that thing is so I can actually use a cool command and this is available in PowerShell

75
00:05:29,580 --> 00:05:32,490
or Bash if you are a Linux user out there.

76
00:05:32,760 --> 00:05:33,840
You can use Cat.

77
00:05:35,430 --> 00:05:40,770
So I'm a new cat and then what I'm going to do is input file to that text.

78
00:05:41,400 --> 00:05:47,070
What the Cat Command does in this case is it's going to show us the contents of that file.

79
00:05:47,070 --> 00:05:49,800
So it's going to show us all the data that's in that file.

80
00:05:50,250 --> 00:05:54,740
I press enter and you notice that that file contains one space, two space three.

81
00:05:54,750 --> 00:05:56,530
So it's got three integers in there, right?

82
00:05:57,240 --> 00:05:59,160
This command has a lot more to it.

83
00:05:59,160 --> 00:06:01,380
If you're really interested in this, you can go look it up.

84
00:06:01,410 --> 00:06:03,120
You can do a lot of cool stuff with it.

85
00:06:03,450 --> 00:06:07,230
It's a really neat command line kind of tool that you can use.

86
00:06:07,230 --> 00:06:12,000
But right now we're using it just in the sense of wanting to see the contents of a file, right?

87
00:06:12,300 --> 00:06:16,560
It's going to print out the contents of the file that we use it with.

88
00:06:16,860 --> 00:06:20,400
So Cat Input File two shows us what's inside of input.

89
00:06:20,400 --> 00:06:20,890
File two.

90
00:06:21,750 --> 00:06:23,910
So just a little kind of side note there.

91
00:06:25,380 --> 00:06:26,820
So that's what we're reading in from.

92
00:06:26,820 --> 00:06:33,870
I'm going to go ahead and then do a my stream and read into just like, see with this info operator

93
00:06:34,200 --> 00:06:35,850
into my end.

94
00:06:36,630 --> 00:06:42,150
And then just to double check, what I'm going to do is I'm going to see out and I'm going to see out

95
00:06:42,600 --> 00:06:43,170
my aunt.

96
00:06:43,890 --> 00:06:46,170
I guess I don't need that.

97
00:06:47,520 --> 00:06:49,260
And then we'll obviously combine.

98
00:06:51,000 --> 00:06:57,390
OK, and one last thing we have to do, I can kind of go back to the presentation here, but we need

99
00:06:57,390 --> 00:07:01,060
to do a mainstream foreclose and why do we do this?

100
00:07:01,080 --> 00:07:03,480
Well, we've opened a input stream.

101
00:07:03,960 --> 00:07:07,590
We kind of need to do a little cleanup process and close it.

102
00:07:07,590 --> 00:07:09,690
So everything is all good to go.

103
00:07:09,870 --> 00:07:15,750
So just remember to do a mainstream foreclose once you're done working with the input stream, once

104
00:07:15,750 --> 00:07:17,190
you're done reading from the file.

105
00:07:18,780 --> 00:07:21,540
All right, so let's go back here and let's finish up with that.

106
00:07:21,540 --> 00:07:23,460
So I'll be on my stream that close.

107
00:07:26,190 --> 00:07:28,050
OK, so pretty cool, right?

108
00:07:28,410 --> 00:07:34,350
I'm going to go ahead and save this and what do you think is going to print to the console if these

109
00:07:34,350 --> 00:07:35,700
are the contents of our file?

110
00:07:36,750 --> 00:07:39,330
We'll go ahead and compile lists.

111
00:07:39,330 --> 00:07:43,480
So how do you file stamped on that CBP dash?

112
00:07:43,850 --> 00:07:46,590
Oh, that's one that you see.

113
00:07:50,430 --> 00:07:52,680
And let's run it, so what do you think's going to print out?

114
00:07:54,920 --> 00:07:59,660
Well, it only printed one out, and that is because we only read one integers, so it saw this first

115
00:07:59,660 --> 00:08:05,360
integer, then there was a space, so it stopped and it just read that into my end and then it printed

116
00:08:05,360 --> 00:08:05,780
that out.

117
00:08:07,710 --> 00:08:12,510
So what if we wanted to read in all three, I can make some other integers, right, so I can say aunt,

118
00:08:13,470 --> 00:08:16,800
my aunt too and aunt, my aunt.

119
00:08:17,070 --> 00:08:17,560
Three.

120
00:08:17,580 --> 00:08:23,040
Sorry, my typing is still a little off because of the wrist thing, but then what I can do is I can

121
00:08:23,040 --> 00:08:31,650
go ahead and read into all of these so I can just say my aunt to and my aunt three.

122
00:08:33,540 --> 00:08:36,450
And then right here I can also print these out.

123
00:08:36,450 --> 00:08:40,560
So I'm just going to copy paste this and I'm not going to print it out with any spaces.

124
00:08:40,560 --> 00:08:42,570
So it's going to be all smashed together.

125
00:08:43,110 --> 00:08:45,000
But that should be OK.

126
00:08:49,570 --> 00:08:52,150
So now what do you think it will print out?

127
00:08:52,420 --> 00:08:57,340
So it's reading this in all spaces, but not printing out spaces, so we should get everything right.

128
00:08:57,350 --> 00:09:04,240
So I'll go ahead and save this and compile it and run it, and we get one to three all smashed together

129
00:09:04,240 --> 00:09:14,350
because we've read all three of the files that were in this file right here, not the files with all

130
00:09:14,350 --> 00:09:17,690
three integers that were in the input file 2016.

131
00:09:19,000 --> 00:09:21,100
So pretty cool, right?

132
00:09:21,310 --> 00:09:24,340
We know how to now read in from files.

133
00:09:25,330 --> 00:09:30,400
So let's go ahead and mess around with some output streams stuff.

134
00:09:30,420 --> 00:09:31,970
So let's check out what that is.

135
00:09:31,980 --> 00:09:33,700
So let's head back to the presentation.

136
00:09:35,560 --> 00:09:42,550
Output streams are declared like this, so rather than IAF stream, it's off stream, so not input file

137
00:09:42,550 --> 00:09:44,020
stream, but output.

138
00:09:44,440 --> 00:09:46,120
Oh, for output file stream, right?

139
00:09:46,780 --> 00:09:50,530
So when we want to write to a file, we do very similar things.

140
00:09:50,530 --> 00:09:55,630
But instead of using the info operator, we will use the output operator.

141
00:09:55,660 --> 00:09:57,430
So this is the input operator here.

142
00:09:58,800 --> 00:10:03,300
So that's what we would use with like CNN, right, and then we will now want to use something like

143
00:10:03,300 --> 00:10:04,800
we would use with the out.

144
00:10:06,250 --> 00:10:07,490
So here I have an example.

145
00:10:07,510 --> 00:10:11,680
This is a little redundant, it's printing out my integer, but we don't care about that much because

146
00:10:11,680 --> 00:10:16,600
we're wanting to probably just check to see that it successfully got written to the file.

147
00:10:17,650 --> 00:10:24,580
We declare an output file stream or assuming that somewhere up here we made an integer my end and we

148
00:10:24,580 --> 00:10:27,390
read into it with an input, right?

149
00:10:27,430 --> 00:10:29,380
So we read into it with an input file stream.

150
00:10:30,400 --> 00:10:35,130
We then open the output stream that we created and we're going to do a similar thing.

151
00:10:35,140 --> 00:10:38,500
We're going to put the file in here that we want to write to this time.

152
00:10:39,930 --> 00:10:46,170
Then what we do is we have the data from the integer variable flow in this direction.

153
00:10:47,330 --> 00:10:51,500
Into the output stream, which will then write it to this file output.

154
00:10:52,880 --> 00:10:54,720
Here we kind of print it redundantly.

155
00:10:54,730 --> 00:10:59,450
We don't need to do that because we can just check to see what's in the file with cat, right?

156
00:11:00,320 --> 00:11:05,750
Then we wrap up by closing the output stream just like we closed our input stream, right?

157
00:11:06,700 --> 00:11:12,220
So let's go back to the editor and see what's going on with this, so I'm going to just add some stuff

158
00:11:12,220 --> 00:11:12,640
in here.

159
00:11:13,060 --> 00:11:21,100
I'm going to say I see Colin Colin 0f stream and I'm going to say my out stream, and I'm actually going

160
00:11:21,100 --> 00:11:27,180
to change this to my in-stream and I'm going to need to update this in a few spots.

161
00:11:27,190 --> 00:11:29,800
So this will be my end, not open.

162
00:11:30,740 --> 00:11:35,660
Be my in-stream right here, and it will be my instinct close.

163
00:11:37,370 --> 00:11:44,440
So what I'm going to do now, though, is I'm just going to open my file down here, my output stream.

164
00:11:44,680 --> 00:11:56,060
I must say my Allah stream that open and I'm going to just say output to tea because I have input file

165
00:11:56,060 --> 00:11:59,690
to write some and a similar naming convention.

166
00:12:00,560 --> 00:12:05,540
And then what I'm going to do is just make it out to their right.

167
00:12:05,540 --> 00:12:09,920
So I'm going to put this operator right here is that you notice is similar to like when we you see out

168
00:12:09,920 --> 00:12:12,530
and all I'm going to do is I'm just going to copy paste.

169
00:12:13,530 --> 00:12:20,730
This stuff right here, right, so I have one operator, so I'm going to do that and I'm actually going

170
00:12:20,730 --> 00:12:27,510
to put a little on, let's just say like, I've got a couple new line characters here or something.

171
00:12:28,290 --> 00:12:29,760
So we have a couple of new lines.

172
00:12:32,190 --> 00:12:36,960
And that should write it out to our stream, and then all I have to do is close, right?

173
00:12:37,620 --> 00:12:42,060
Some of my output stream got close and we should be good to go.

174
00:12:42,190 --> 00:12:43,800
I'm going to go ahead and save this.

175
00:12:47,860 --> 00:12:54,940
Let's go ahead and compile and run this, and it's going to print out, right?

176
00:12:55,420 --> 00:13:02,260
And then what we want to do is verify that the same thing that gets printed out right here is actually

177
00:13:02,260 --> 00:13:03,760
what's inside of our file.

178
00:13:03,770 --> 00:13:06,070
So we'll use the command again to verify that.

179
00:13:06,070 --> 00:13:09,910
So I run it, it prints out one two three and then what?

180
00:13:09,910 --> 00:13:12,510
I can use cat output to that text.

181
00:13:13,360 --> 00:13:16,450
And we noticed that we have one two three.

182
00:13:16,690 --> 00:13:21,730
But to differentiate it a little bit, you notice there's two new lines, and that's why you noticed

183
00:13:21,730 --> 00:13:22,740
these two new lines.

184
00:13:22,750 --> 00:13:29,380
Unlike when we just ran the program, right when we ran the program, it just said one two three.

185
00:13:31,990 --> 00:13:34,390
Now you notice the prints out to new lines.

186
00:13:35,860 --> 00:13:37,240
So pretty cool.

187
00:13:37,420 --> 00:13:39,940
We now know how to write to a file.

188
00:13:40,510 --> 00:13:42,790
I have another example file here, though.

189
00:13:43,510 --> 00:13:46,570
Go ahead and clear this and I'm going to count this other file.

190
00:13:46,570 --> 00:13:48,710
It's just input file that text.

191
00:13:49,570 --> 00:13:54,430
You notice that it actually has like a little sentence, a string and says here is an input file.

192
00:13:54,460 --> 00:13:57,560
So what if we now wanted to read into a string?

193
00:13:57,580 --> 00:14:06,130
So I'm going to go ahead and mix this up here and put a silly string my string and I'm going to read

194
00:14:06,130 --> 00:14:09,490
into my strings and I'm going to say my string.

195
00:14:11,070 --> 00:14:13,440
And then I'm not going to print this out any more.

196
00:14:13,980 --> 00:14:21,630
What I'm going to do is just read into there and then I'm going to put it into my Airstream, so I will

197
00:14:21,630 --> 00:14:23,010
say my screen.

198
00:14:24,840 --> 00:14:26,970
And now we can read in the stream, right?

199
00:14:27,300 --> 00:14:28,890
So should be pretty cool.

200
00:14:28,890 --> 00:14:31,380
Let's go ahead and run it and see how that works.

201
00:14:33,270 --> 00:14:43,230
So I go ahead and I run it, and then let's go ahead and cap the output output 2.60 or this time.

202
00:14:43,920 --> 00:14:48,790
I actually sorry, I need to rerun this because I realize I did not change it to amplify that.

203
00:14:49,290 --> 00:14:50,850
So let's go ahead and rerun that.

204
00:14:52,900 --> 00:14:54,370
So I saved it.

205
00:14:54,940 --> 00:15:00,400
I'm going to compile and I'm going to rerun it because we still had input file to this time, I want

206
00:15:00,400 --> 00:15:05,320
to read from that input file without the two that has the sentence, right?

207
00:15:06,010 --> 00:15:10,630
So I run that and then now let's go ahead and see what's in our output to that text.

208
00:15:10,810 --> 00:15:12,990
We had one two three in there, right?

209
00:15:13,000 --> 00:15:14,050
And then two new lines.

210
00:15:14,080 --> 00:15:15,070
Now let's see what's in it.

211
00:15:16,180 --> 00:15:20,620
So you notice it just says here and it has some funky things for the new lines here.

212
00:15:21,610 --> 00:15:33,730
If I was to go into a dash and I left the cat the outfit to the text, we noticed we see the hue with

213
00:15:33,730 --> 00:15:39,320
the new lines in there so we can verify that it now has changed, right?

214
00:15:39,330 --> 00:15:41,160
But the thing is, we just read the word here.

215
00:15:41,170 --> 00:15:45,940
We really wanted like this whole sentence, right?

216
00:15:45,940 --> 00:15:47,260
But we only got this first one.

217
00:15:47,260 --> 00:15:48,550
Why do we only get the first one?

218
00:15:48,550 --> 00:15:52,010
Because strings just read until whitespace by default here.

219
00:15:52,030 --> 00:15:54,460
So we read the here we hit whitespace.

220
00:15:54,460 --> 00:15:56,050
It stopped, and that's all it read in.

221
00:15:56,590 --> 00:15:59,470
We do know how to read a full sentence, though, right?

222
00:15:59,470 --> 00:16:01,190
And that is using get line.

223
00:16:01,210 --> 00:16:06,670
And the cool thing is we can totally use get line with this input stream, just like we have with scene.

224
00:16:07,450 --> 00:16:09,270
So let's go ahead and do that.

225
00:16:09,280 --> 00:16:14,170
I'm actually going to get rid of these instances and open a new terminal

226
00:16:17,380 --> 00:16:19,420
and let's go ahead and modify this.

227
00:16:19,420 --> 00:16:25,720
So rather than reading into this right here, I'm actually going to do a speed control and get line.

228
00:16:26,650 --> 00:16:31,360
And in here, I'm not going to do scene and my string like that.

229
00:16:31,360 --> 00:16:38,020
I'm actually going to replace C rn with my in-stream right.

230
00:16:38,020 --> 00:16:40,660
So my end is going to be right here.

231
00:16:40,660 --> 00:16:42,250
So we're reading from that file.

232
00:16:42,820 --> 00:16:44,890
Let's go ahead and save.

233
00:16:45,800 --> 00:16:49,490
And after saying here, I'm going to go ahead and compile.

234
00:16:52,610 --> 00:17:01,820
So far, Change Wanda, CVP show this one, the BBC, and now I'm going to what it looks like we have

235
00:17:01,820 --> 00:17:02,390
a.

236
00:17:04,730 --> 00:17:07,010
Problem within my in-stream.

237
00:17:08,750 --> 00:17:09,550
Foreclosed.

238
00:17:09,740 --> 00:17:10,640
So I just didn't.

239
00:17:10,850 --> 00:17:14,540
Actually, it's with this, I just did not put a semicolon.

240
00:17:14,750 --> 00:17:17,090
So let's go ahead and try that again.

241
00:17:17,930 --> 00:17:19,490
Now I run fs one.

242
00:17:19,730 --> 00:17:20,240
You see?

243
00:17:20,900 --> 00:17:21,260
Oh.

244
00:17:23,430 --> 00:17:27,110
So I run that, let's go ahead and can't output to that.

245
00:17:29,130 --> 00:17:32,600
So you notice now it has the full sentence and I can go into bash.

246
00:17:32,760 --> 00:17:35,970
Kind of get these weird characters for the new lines there.

247
00:17:35,970 --> 00:17:43,230
But if I go over here and bash, let's go ahead and cat output to see.

248
00:17:43,230 --> 00:17:45,090
Hopefully this shows up in there.

249
00:17:45,340 --> 00:17:47,430
So it says here's an input file.

250
00:17:47,430 --> 00:17:53,400
We've read everything into it now, so we can do that.

251
00:17:53,520 --> 00:17:56,760
Now I logout, bash and back into PowerShell.

252
00:17:58,590 --> 00:18:04,860
So pretty cool, right, we can actually read in with get line and we can just kind of standard read

253
00:18:04,860 --> 00:18:06,900
in from the stream like that as well.

254
00:18:09,390 --> 00:18:12,600
So this is all that I am going to go over right now.

255
00:18:12,630 --> 00:18:18,870
But what we're going to get into is actually using loops with the stream as well, so.

256
00:18:20,080 --> 00:18:24,850
Think about that because I'm going to have a problem in the next lecture, we're going to learn kind

257
00:18:24,850 --> 00:18:36,100
of one more new concept and you could use a loop and have something like my in-stream into my string

258
00:18:36,280 --> 00:18:38,250
and you could just keep reading strings.

259
00:18:38,260 --> 00:18:43,510
Imagine this is inside of some wow loop or something like that, right?

260
00:18:44,640 --> 00:18:47,520
So that is something that we're going to look into.

261
00:18:47,730 --> 00:18:52,710
And with that, I will go ahead and see you in the next lecture.
