WEBVTT

00:00.870 --> 00:03.900
Hello again! In this video, we are going to look at file modes.

00:06.350 --> 00:10.550
When we open a file in C++, there are a number of options we can use.

00:10.910 --> 00:13.310
These options are known as "file modes".

00:13.700 --> 00:15.950
These are basically the same as they are in Unix.

00:15.950 --> 00:20.300
So if you knew about file modes in Unix, you probably know all this!

00:22.560 --> 00:27.000
To use a file mode, we pass it as an optional second argument when we open the file.

00:27.600 --> 00:31.680
So that is when we call the constructor for stream or the open member function.

00:35.450 --> 00:40.730
There are two defaults, which are to use text mode and truncate mode. Text mode means that the

00:40.730 --> 00:44.220
characters are stored in the file as ASCII.

00:44.240 --> 00:52.160
So for example, if we write 42 to a file, the file will store that as the ASCII code for the character

00:52.160 --> 00:54.020
four and the character two.

00:55.070 --> 01:00.920
So those will be the corresponding binary numbers, which I think are 100 and 98.

01:03.150 --> 01:04.110
But don't quote me on that!

01:06.190 --> 01:11.290
Truncate mode means that all the data that is in the file is overwritten, so that is for output files

01:11.290 --> 01:13.600
only. Obviously input files will not be truncated.

01:14.290 --> 01:17.590
So when we open a file, it is opened in truncate mode by default.

01:17.860 --> 01:23.650
All the data is erased. And when we write to the file, that will start writing at the beginning of the

01:23.650 --> 01:24.040
file.

01:28.810 --> 01:34.300
There is also an append mode. So this will preserve the data that is in the file.

01:34.720 --> 01:40.630
And when we write data to the file, this will appear at the end of the file. So it will go after the

01:40.630 --> 01:42.160
data that was already in the file.

01:43.780 --> 01:49.900
To do this, we pass fstream double colon app as the second argument to open.

01:50.680 --> 01:52.410
This is a bitmask.

01:52.420 --> 01:53.530
It is a member of fstream.

01:53.860 --> 01:59.090
Well, actually, it is a member of ios, but you can use fstream or ios here.

02:01.400 --> 02:06.380
So this will open the file in append mode, the data in the file will be preserved and the

02:07.490 --> 02:11.150
write operation will add data at the end of the file.

02:13.130 --> 02:15.080
So here is some code to try that out.

02:15.680 --> 02:19.240
We have an important file, we open it in append more.

02:20.030 --> 02:22.130
And let's see if this file really is important.

02:25.320 --> 02:27.840
"Important data - do not remove!"

02:28.260 --> 02:29.400
Yes, that sounds important!

02:30.980 --> 02:31.740
We have to be careful here.

02:33.540 --> 02:40.650
So we open this file in append mode and then we write to the file using the left shift operator.

02:41.250 --> 02:46.680
We only use the write member function for low-level work, which we do not need here.

02:47.130 --> 02:48.300
So let's run this program.

02:51.620 --> 02:57.170
So the program has run. It has written to the file, we presume, and let's see if our important data is

02:57.170 --> 02:57.740
still there.

02:59.560 --> 02:59.890
Phew!

03:02.910 --> 03:05.520
So there we are. "Important data - do not remove!"

03:05.880 --> 03:10.110
So that is the data that was there before. And then we have "some more data" that we wrote at the end.

03:10.860 --> 03:12.750
So that has worked. Good.

03:16.240 --> 03:21.970
Instead of text mode, we can use binary mode, so the data in the file will have exactly the same bit

03:21.970 --> 03:23.740
patterns as the data in memory.

03:24.220 --> 03:30.250
So if we store the number 42 in a binary file, it will be stored as a binary number, which has

03:30.250 --> 03:36.400
the equivalent to 42. The decimal 42, binary 101010.

03:38.680 --> 03:44.500
If we used text mode, that would be stored as two separate characters, one representing '4', and one

03:44.500 --> 03:45.310
representing '2'.

03:47.780 --> 03:54.290
Binary mode is more complicated than text mode. It is low level, and it is easy to get things wrong.

03:55.220 --> 03:59.690
You could also have problems when you try to take binary files from one computer to another.

04:00.170 --> 04:05.690
If the computer that the file was written on has a different architecture from the computer that's opening

04:05.690 --> 04:12.350
the file, it may not read the data correctly. Or even the same compiler with different settings, because

04:12.350 --> 04:15.200
optimisation can change the way that the data is laid out.

04:15.770 --> 04:17.810
So generally, you should avoid binary data.

04:18.620 --> 04:24.140
However, there are some cases where it is important. If you are working with data, which is naturally

04:24.140 --> 04:24.620
binary.

04:24.980 --> 04:32.180
If you're working with file formats, so things like image files, media files, videos and so on.

04:34.760 --> 04:37.880
And we will have an example of using binary files later on.

04:42.510 --> 04:50.490
There are some other file modes. Truncate will always use truncate mode. In will open the file for input.

04:50.970 --> 04:55.650
So we have ifstream, which is an input stream and ofstream, which is an output file stream.

04:56.130 --> 05:00.210
There is also an fstream which is a general file stream, and you can use it for either.

05:00.900 --> 05:06.030
So you can use in and out to say whether you want to use it for reading or writing or both.

05:06.660 --> 05:13.530
So in will open it for input, out will open it for output. And that will also put it into truncate mode even

05:13.530 --> 05:14.880
if you did not ask for it.

05:16.750 --> 05:19.080
There is also ate or at 'e'.

05:19.120 --> 05:21.430
I do not know how you are supposed to pronounce it!

05:22.090 --> 05:23.470
This is similar to append.

05:23.860 --> 05:26.350
So it'll preserve the existing data in the file.

05:26.800 --> 05:30.640
And by default, if you write data, it will appear after the end of the file.

05:31.150 --> 05:35.980
However, you can also write the output somewhere else in the file if you want to, which you cannot

05:35.980 --> 05:36.730
do with append.

05:37.120 --> 05:39.160
Append always has to go at the end of the file.

05:45.400 --> 05:51.340
If he wants to combine different modes, these are just bitmasks, so we can use bitwise 'or', the

05:51.340 --> 05:52.720
vertical bar, to combine them.

05:53.410 --> 05:59.920
So if we have an fstream and we want to open it for output in append mode, then we combine the out and

05:59.920 --> 06:00.820
append modes.

06:02.200 --> 06:06.590
So this will open "important.txt" for output, in append mode.

06:07.270 --> 06:12.140
So that is exactly equivalent to the way that we were using the file in the example.

06:15.640 --> 06:21.490
We can also combine in and out with an fstream, and that will give us an fstream, that can both read from a file

06:21.490 --> 06:22.570
and write to it.

06:23.470 --> 06:25.900
If we do that, the file is not going to be truncated.

06:26.380 --> 06:32.410
Usually when you do this, you want to read from the file before you write to the data, so it does not

06:32.410 --> 06:34.570
make sense to overwrite the data before you look at it.

06:35.950 --> 06:41.620
If you do want to truncate it, then you can combine that as well, so we are opening "both.txt" for

06:41.620 --> 06:44.170
input and output in truncate mode.

06:46.830 --> 06:49.520
And then finally, there are some restrictions on how you can use mode.

06:50.280 --> 06:53.190
You can only use out with fstream and ofstream.

06:53.200 --> 06:57.720
You cannot use it with input stream, obviously, and you cannot use in with an [output] stream.

06:58.590 --> 07:04.200
Trunc is only available in output mode and you cannot use append and trunc together.

07:05.580 --> 07:08.880
If you do use app, the file will always be opened in output mode.

07:10.670 --> 07:11.690
So there we are!

07:12.170 --> 07:13.700
That's it for this video.

07:14.270 --> 07:17.420
I'll see you next time, but meanwhile, keep coding!
