1
00:00:01,490 --> 00:00:02,180
Hi everyone.

2
00:00:02,180 --> 00:00:07,090
So from this lesson we will start learning file handling in C++.

3
00:00:07,130 --> 00:00:10,910
Now in this world we will learn this topic writing to a file.

4
00:00:11,600 --> 00:00:13,740
So for writing to a file the syntax.

5
00:00:13,800 --> 00:00:16,010
What do you have to lose so for writing the code.

6
00:00:16,010 --> 00:00:23,870
First of all you have to include a file and the name of the file is f a stream file stream so after

7
00:00:23,870 --> 00:00:28,780
including the file so it is obvious that we will write the code inside me.

8
00:00:28,780 --> 00:00:33,700
Then what we have to do so the name of the class is o f stream output file the stream.

9
00:00:33,700 --> 00:00:34,660
So this is a class.

10
00:00:34,690 --> 00:00:37,410
And what you have to do is we have to create an object.

11
00:00:37,420 --> 00:00:40,030
Let's say the name of the object is output file.

12
00:00:40,930 --> 00:00:43,420
I would file is the name of the object.

13
00:00:43,540 --> 00:00:45,910
And so what this object will do.

14
00:00:45,930 --> 00:00:50,810
I am calling a constructor so for calling the constructor you have to give a file name so I'm giving

15
00:00:50,830 --> 00:00:52,890
the file in my final dirty.

16
00:00:53,110 --> 00:01:03,750
So suppose there's a file and the name of this file is my file in my file dart the 60 so after writing

17
00:01:03,750 --> 00:01:06,570
this piece of code what will happen.

18
00:01:06,570 --> 00:01:09,910
So there is a connection between this object and this file.

19
00:01:10,050 --> 00:01:15,420
So a connection will get established between this object and this file.

20
00:01:16,440 --> 00:01:19,220
So basically you will do anything with this object.

21
00:01:19,260 --> 00:01:22,480
Then the changes will get reflected inside this file.

22
00:01:23,730 --> 00:01:29,140
So whatever you're going to do with this object the changes are going to be reflected in the file.

23
00:01:29,160 --> 00:01:31,440
My friend not the extreme.

24
00:01:31,470 --> 00:01:34,240
Now there are two things that can happen.

25
00:01:34,260 --> 00:01:36,240
So after writing this line.

26
00:01:36,360 --> 00:01:38,820
So when you will write this line then you will execute this line.

27
00:01:38,820 --> 00:01:40,500
There are two possible situation.

28
00:01:40,500 --> 00:01:44,050
So the first situation is this file already exists.

29
00:01:44,370 --> 00:01:47,320
This file already exists.

30
00:01:47,370 --> 00:01:49,350
So what this statement will do.

31
00:01:49,380 --> 00:01:51,020
So this statement will open the file.

32
00:01:51,030 --> 00:01:57,390
So if the file already exist then open the file then open the file.

33
00:01:57,430 --> 00:02:00,670
Now the second situation is this file does not exist.

34
00:02:01,120 --> 00:02:06,410
So if this file does not exist then this piece of code what will do with this line what it will do.

35
00:02:06,490 --> 00:02:07,850
It will create the file.

36
00:02:08,020 --> 00:02:13,060
So it will create file with the name my file not the 60s.

37
00:02:13,060 --> 00:02:19,830
So I'll finally be created with this name my file not the 60 and then open that file.

38
00:02:19,930 --> 00:02:24,200
So cleared file and then open that file so cleared file and open file.

39
00:02:24,370 --> 00:02:25,580
I'm everything myself.

40
00:02:25,660 --> 00:02:28,210
So this line can do two things.

41
00:02:28,210 --> 00:02:29,890
So if the file already exists.

42
00:02:30,220 --> 00:02:33,970
So if this file already exists then open the fight.

43
00:02:34,420 --> 00:02:38,140
And if this file does not exist then basically cleared the file with this name.

44
00:02:38,140 --> 00:02:42,600
My file not the 60 and then open that file simple.

45
00:02:42,640 --> 00:02:44,810
Now let us discuss next thing.

46
00:02:44,830 --> 00:02:46,600
So what can happen.

47
00:02:46,630 --> 00:02:49,090
So for example this is your file.

48
00:02:49,120 --> 00:02:57,000
My file not the 60.

49
00:02:57,030 --> 00:03:02,330
So basically if you will do something like this Let's say you are trying to write something to a file.

50
00:03:02,340 --> 00:03:11,140
So if you do this out file you want to insert you want to write Hello world inside this file hello world.

51
00:03:11,300 --> 00:03:13,710
And Andy.

52
00:03:13,870 --> 00:03:18,160
So what will happen the effect of this line is go to the file and write Hello world.

53
00:03:18,610 --> 00:03:23,030
So this file can be empty so the first scenario is this file can be empty.

54
00:03:23,110 --> 00:03:25,690
The other scenario is this file list containing some data

55
00:03:30,020 --> 00:03:36,640
so this file might be containing some data so if you execute this piece of code.

56
00:03:36,730 --> 00:03:39,310
So if this is the situation then obviously what will happen.

57
00:03:39,310 --> 00:03:43,080
So Hallowell will get printed here.

58
00:03:43,270 --> 00:03:48,350
Now if this is the situation your file is already containing some data then what this line will do.

59
00:03:48,370 --> 00:03:50,710
So this line will actually truncate.

60
00:03:50,710 --> 00:03:59,450
So this line will actually delete all your data and then it will insert Hallowell so all the data will

61
00:03:59,450 --> 00:04:06,010
be deleted and then this string will get inserted.

62
00:04:06,010 --> 00:04:11,240
Now if you want to preserve the data if you want to preserve your oil they don't you do not want their

63
00:04:11,250 --> 00:04:12,570
oil later to be removed.

64
00:04:12,570 --> 00:04:17,070
So what do you have to do you have to pass of one what barometer in the constructor.

65
00:04:17,380 --> 00:04:21,070
Basically we have so the second parameter can be.

66
00:04:21,460 --> 00:04:22,870
So we'll hear what I'm doing.

67
00:04:22,910 --> 00:04:32,930
I am writing off a stream then the name of the object is out file the name of the file is my file not

68
00:04:32,920 --> 00:04:35,090
the 60.

69
00:04:35,270 --> 00:04:37,850
Now the second barometer can be one of the two things.

70
00:04:37,850 --> 00:04:40,370
So the second parameter can be us.

71
00:04:40,370 --> 00:04:44,960
Column Column D R U N C so D2 and C means truncate.

72
00:04:45,440 --> 00:04:52,220
So this is by default if you will not give the second barometer then this is the second barometer by

73
00:04:52,220 --> 00:04:53,290
default.

74
00:04:53,630 --> 00:04:55,830
And if you want to preserve the content.

75
00:04:55,830 --> 00:04:58,450
So if you want to preserve the content do you have to write something like this.

76
00:04:58,450 --> 00:05:05,250
IOW column Column B so APB stands for append the data so this all thing is Same here.

77
00:05:05,970 --> 00:05:07,110
So there are two types.

78
00:05:07,140 --> 00:05:09,960
So the second parameter can be truncate.

79
00:05:09,990 --> 00:05:11,040
So this is by default.

80
00:05:11,070 --> 00:05:12,910
So I have not written truncate here.

81
00:05:13,230 --> 00:05:18,870
So what this line will do if you want to insert something into the file it will delete your all data

82
00:05:18,960 --> 00:05:20,410
and then it will insert Hallowell.

83
00:05:20,610 --> 00:05:26,090
But if you want to preserve the data if you want to preserve the data then you have to write a US column

84
00:05:26,100 --> 00:05:27,190
column append.

85
00:05:27,330 --> 00:05:33,300
So what will happen if a file is already containing some data then Hallowell will get inserted at the

86
00:05:33,300 --> 00:05:34,380
end.

87
00:05:34,430 --> 00:05:35,070
OK.

88
00:05:35,250 --> 00:05:44,150
So by default this is truncate and if you want to present the data I will use append APB.

89
00:05:44,170 --> 00:05:46,900
So this is just an index for driving data to a file.

90
00:05:46,930 --> 00:05:48,520
Similarly you can write more data.

91
00:05:48,520 --> 00:05:50,950
For example you can write out file.

92
00:05:51,010 --> 00:05:53,430
Let's say you want to insert it in teachers or you can write.

93
00:05:53,560 --> 00:05:57,500
I would file 50 and line similarly.

94
00:05:57,540 --> 00:05:58,960
You can write it like this.

95
00:05:58,980 --> 00:06:00,720
I would file.

96
00:06:00,720 --> 00:06:09,030
Let's say you want to insert the branch of the student so you can insert CSC and line.

97
00:06:09,050 --> 00:06:12,450
So in this way this is this index for writing into a file.

98
00:06:12,470 --> 00:06:15,470
Now after writing to the file after your work is or what.

99
00:06:15,650 --> 00:06:19,520
The most important thing that we have to do is basically close the connection.

100
00:06:19,610 --> 00:06:23,350
So you have to write you have to call the function dot close.

101
00:06:23,420 --> 00:06:25,790
So the name of the function is close.

102
00:06:25,790 --> 00:06:30,830
So after doing all the work that you want to do with the file you have to call the function goes.

103
00:06:30,860 --> 00:06:33,600
So what this close function will do it will.

104
00:06:33,620 --> 00:06:38,010
So this is the connection so this connection will get a break.

105
00:06:38,080 --> 00:06:44,590
This connection will be broken if you will call the function close and it is very important.

106
00:06:44,620 --> 00:06:50,050
It is a very good practice also and it is very important also to call the function close.

107
00:06:50,050 --> 00:06:54,320
Now what will happen if you will not call this function.

108
00:06:54,360 --> 00:06:57,710
So why this function is important why calling of this function is important.

109
00:06:57,750 --> 00:07:05,230
So you have seen many times for example when you connect pen drive to be a computer and then if you

110
00:07:05,230 --> 00:07:06,860
want to eject to a pen drive.

111
00:07:06,880 --> 00:07:11,080
So after connecting the bend over to a computer if you want to eject you up and drive then sometimes

112
00:07:11,080 --> 00:07:15,210
an added pop ups that some program might be using this pen drive.

113
00:07:15,220 --> 00:07:19,330
So you can not eject and that is because of this thing.

114
00:07:20,080 --> 00:07:24,590
So the developed but forgot to write this line output finally got close.

115
00:07:24,670 --> 00:07:26,920
So that's why the connection is still there.

116
00:07:26,950 --> 00:07:32,950
So if the connection is it still did if the connection is still there then you will get this idea that

117
00:07:33,160 --> 00:07:36,090
some program is using your pen drive.

118
00:07:36,120 --> 00:07:38,120
So you cannot eject.

119
00:07:38,310 --> 00:07:41,940
So what you're doing those scenarios you basically lose that program.

120
00:07:41,940 --> 00:07:44,820
So if the program is closed then what will happen.

121
00:07:44,820 --> 00:07:49,650
So this connection will be broken disconnection will break.

122
00:07:49,680 --> 00:07:51,720
And you can eject you up and drive.

123
00:07:51,760 --> 00:07:55,420
So it is always a good practice after your work is done with the file.

124
00:07:55,420 --> 00:08:00,920
You have to call the closed function so in the next we do we will write the code.

125
00:08:01,170 --> 00:08:02,680
I will see in the next one maybe.
