WEBVTT 00:00:08.700 --> 00:00:15.030 Right now we're going to see how to write content inside our file using the right function. 00:00:15.320 --> 00:00:15.870 OK. 00:00:15.870 --> 00:00:19.870 So we going to write inside the text file. 00:00:19.890 --> 00:00:22.050 Any content you want using this function. 00:00:22.050 --> 00:00:26.670 So first of all let's have our page be tag. 00:00:27.630 --> 00:00:33.900 So first we are going to we need to open the file so we can't use the f right function without opening 00:00:33.900 --> 00:00:34.390 the file. 00:00:34.520 --> 00:00:41.320 Ok so you get to open the file and place the reference inside the variable file handle. 00:00:42.300 --> 00:00:52.170 Ok so when I said this to f open I'm going to place the name of our file or the path of the file. 00:00:52.440 --> 00:01:00.520 Some text book texts and we are going to open the file in arising mode and that's going to be doubly 00:01:00.590 --> 00:01:00.840 . 00:01:01.090 --> 00:01:12.330 OK so all if we can't open we're going to we're going to print on the screen unable to open a file and 00:01:12.330 --> 00:01:13.510 then we subscripts. 00:01:13.770 --> 00:01:14.720 OK. 00:01:15.510 --> 00:01:20.980 OK so now to write in The File we're going to use the f right function OK. 00:01:21.000 --> 00:01:25.310 And you can click on control K to explore details about this function. 00:01:25.550 --> 00:01:26.580 OK. 00:01:27.830 --> 00:01:35.130 So who's going to take us first promiser the results are going to be our file handle and as the second 00:01:35.130 --> 00:01:40.980 barometer the string that we want to write inside the file. 00:01:41.070 --> 00:01:48.150 Ok so so desperate plays the arguments inside the function. 00:01:48.150 --> 00:01:53.680 So the first one is going to be the resource file handle. 00:01:54.210 --> 00:01:56.100 And the second one. 00:01:56.550 --> 00:01:58.350 Let's write a string for example. 00:01:58.350 --> 00:02:01.960 This is new contents. 00:02:03.450 --> 00:02:06.140 OK. 00:02:06.360 --> 00:02:08.160 Or Die 00:02:10.070 --> 00:02:13.040 . 00:02:17.130 --> 00:02:18.570 Unable to write 00:02:21.780 --> 00:02:22.140 in. 00:02:22.140 --> 00:02:25.550 File or write to file. 00:02:26.510 --> 00:02:28.210 OK. 00:02:28.920 --> 00:02:31.850 All right so. 00:02:34.290 --> 00:02:35.770 So let's see now. 00:02:36.180 --> 00:02:42.490 So after we have executed these statements let's see what the contents of the file is going to be. 00:02:42.660 --> 00:02:51.540 So let's declare or let's set the file contents valuable to do is use the file function. 00:02:51.810 --> 00:02:58.020 Ok then let's read what's what's inside our file 00:03:01.530 --> 00:03:10.640 or by unable to read file. 00:03:12.890 --> 00:03:14.760 Ok so I save and refresh 00:03:14.920 --> 00:03:22.130 . 00:03:30.130 --> 00:03:35.210 OK we need to dump the variables and we can see the contents. 00:03:35.640 --> 00:03:36.240 OK 00:03:43.170 --> 00:03:49.610 OK so we see that the new contents of our file is only one line. 00:03:49.620 --> 00:03:53.700 OK so the file function returns an array of 1 elements. 00:03:54.000 --> 00:03:55.400 So of course only one line there. 00:03:55.410 --> 00:04:04.200 So what's happened here is that the F. writes function deleted all the contents of our file so if we 00:04:04.260 --> 00:04:07.110 refresh brackets using a 5 00:04:26.600 --> 00:04:32.670 we still need to open the file from brackets 00:04:36.010 --> 00:04:41.790 open with brackets. 00:04:41.790 --> 00:04:46.110 Now we can see that we of course only one line inside our file. 00:04:46.110 --> 00:04:56.800 Ok so the Everetts function just deleted everything OK and replaced all that with whatever we have it 00:04:57.320 --> 00:05:00.950 puts in the promises of the F. writes function. 00:05:01.140 --> 00:05:03.130 OK let's open it again. 00:05:03.130 --> 00:05:06.860 Our file file handling 00:05:11.930 --> 00:05:13.940 . 00:05:14.250 --> 00:05:22.250 OK so now we're going to use another function to change the content of files its file its contents. 00:05:22.380 --> 00:05:23.560 OK. 00:05:23.570 --> 00:05:32.610 So just one thing you mention here that we don't have a gap between our body and the future. 00:05:32.830 --> 00:05:40.330 If you remember we said the emerging bottom of the body to 60 pixels which is exactly the same as the 00:05:40.330 --> 00:05:43.920 height of our pixels of our of water. 00:05:44.000 --> 00:05:44.580 OK. 00:05:44.750 --> 00:05:51.250 So if you want to have a big up to have some gap there you just need to have a margin bottom for the 00:05:51.250 --> 00:05:58.290 body bigger than the height than the height of our future because the side is having that position. 00:05:58.350 --> 00:05:59.510 Absolutes OK. 00:05:59.690 --> 00:06:01.780 And he's sticking to the bottom anyway. 00:06:02.440 --> 00:06:06.980 So let me do this quickly before we continue to show you. 00:06:07.130 --> 00:06:09.040 So he's going to open the water. 00:06:09.510 --> 00:06:15.210 I'm going to set the margin for example to 100 pixels. 00:06:16.190 --> 00:06:17.170 If you see words 00:06:20.500 --> 00:06:24.770 if I refresh the page you can see that we've got some gap there. 00:06:24.970 --> 00:06:25.350 OK. 00:06:25.450 --> 00:06:27.780 So how to pick cells from there to there. 00:06:28.030 --> 00:06:28.590 OK. 00:06:28.610 --> 00:06:31.940 And the height of our foot is only 60 pixels. 00:06:32.380 --> 00:06:32.790 OK. 00:06:33.010 --> 00:06:36.120 Anyway let's continue on the file puts content. 00:06:36.220 --> 00:06:48.480 So now this new function is going to change the contents add some contents to our file. 00:06:48.550 --> 00:06:49.140 OK. 00:06:49.270 --> 00:06:52.530 So let's see how to use these function have our BHB TYG first 00:06:57.000 --> 00:07:03.040 and the function is file put contents. 00:07:03.330 --> 00:07:06.670 OK. 00:07:06.800 --> 00:07:08.140 And we're going to give it's 00:07:11.890 --> 00:07:14.360 a couple of purposes for now. 00:07:15.150 --> 00:07:16.160 OK. 00:07:17.080 --> 00:07:21.400 So the first part I would say is going to be the file name or the file pads with the name. 00:07:21.400 --> 00:07:24.720 So they go some text or text. 00:07:24.880 --> 00:07:25.500 OK. 00:07:25.510 --> 00:07:29.680 And the second problem so we're going to place anything that we want to add to our file. 00:07:29.680 --> 00:07:33.030 So for example let's. 00:07:33.090 --> 00:07:37.420 Right. 00:07:39.430 --> 00:07:50.020 So I would return and then this is some other new content. 00:07:51.340 --> 00:07:53.130 OK. 00:07:54.530 --> 00:08:04.790 OK so all die are going to say unable to write to file. 00:08:04.930 --> 00:08:17.890 Ok so if you try this day and place the contents inside the file contents variable using the file function 00:08:19.150 --> 00:08:23.000 can use exactly the same code there. 00:08:25.060 --> 00:08:27.630 Then the variable again. 00:08:28.570 --> 00:08:43.120 So if we refresh we can see that this function also raised everything inside the textfile and replace 00:08:43.120 --> 00:08:46.490 this with the string that we have put in there. 00:08:46.730 --> 00:08:48.950 OK. 00:08:49.540 --> 00:08:50.320 All right. 00:08:50.330 --> 00:08:59.420 So to avoid that if you want only to append some content to the file we need to add another parameter 00:08:59.620 --> 00:09:03.100 to the file its contents function. 00:09:03.130 --> 00:09:05.820 So we're going to have a third parameter. 00:09:06.080 --> 00:09:09.080 I'm going to say file. 00:09:09.440 --> 00:09:09.970 OK. 00:09:10.190 --> 00:09:10.680 Wow. 00:09:10.690 --> 00:09:13.320 And it's called append all in capital letters. 00:09:13.350 --> 00:09:13.750 OK. 00:09:13.930 --> 00:09:21.830 So if we do that this time it is going to append any content that we are placed in the second promiser 00:09:21.840 --> 00:09:22.150 . 00:09:22.450 --> 00:09:24.100 OK so let's try this now. 00:09:24.350 --> 00:09:36.730 So if we refresh the page Okay so you can see that we have some more content in that. 00:09:36.970 --> 00:09:38.360 OK. 00:09:38.970 --> 00:09:40.260 Well let's have a new line. 00:09:40.280 --> 00:09:43.390 This is the backslash. 00:09:43.450 --> 00:09:49.730 And so refresh the page and then we can see that we have 00:09:53.100 --> 00:09:55.120 we have two lines. 00:09:55.820 --> 00:09:57.150 OK. 00:09:58.020 --> 00:10:05.110 As the arrays returning to we've got two elements in the array. 00:10:05.120 --> 00:10:07.030 So that's the first elements 00:10:10.270 --> 00:10:13.620 and that's the second element. 00:10:15.500 --> 00:10:22.910 OK so at the beginning since we kept the Everetts function so the F. writes function has just written 00:10:23.470 --> 00:10:28.930 this inside of files or at the beginning of the file is only one line. 00:10:29.290 --> 00:10:30.000 OK. 00:10:30.010 --> 00:10:32.560 After the F. writes function execution. 00:10:32.920 --> 00:10:41.270 It's one line and then after that we have added this to this line case. 00:10:41.450 --> 00:10:42.970 We what we have in your line. 00:10:43.190 --> 00:10:43.980 All right. 00:10:44.290 --> 00:10:50.660 And now we can see that we are having two lines using the file contents function. 00:10:51.040 --> 00:11:05.260 Ok so that was a quick overview about how to handle files in BHB how to open close read writes in files 00:11:05.260 --> 00:11:05.430 . 00:11:05.460 --> 00:11:11.860 Ok in the next video we're going to learn how to upload files to the server