WEBVTT 00:00:06.250 --> 00:00:12.400 In this video we're going to learn how to handle files using BHB. 00:00:12.480 --> 00:00:18.420 First of all we're going to learn how to open and close files and then we're going to learn how to get 00:00:18.420 --> 00:00:26.070 the content of files using the F read or file contents function or the file function. 00:00:26.130 --> 00:00:34.170 And finally we will see how we can change the content of files using the f right or the file contents 00:00:34.500 --> 00:00:35.880 function. 00:00:36.840 --> 00:00:46.480 Ok so the file We're going to be playing with is going to be our text file some text or text. 00:00:46.620 --> 00:00:47.970 OK. 00:00:48.750 --> 00:00:56.480 If you open this file using control then it's going to open using the notepad editor. 00:00:56.550 --> 00:00:57.920 If your windows. 00:00:57.930 --> 00:00:59.080 OK. 00:00:59.670 --> 00:01:10.950 So to open the file using brackets you can click right and go to open with and select you can use brackets 00:01:10.950 --> 00:01:18.540 but that's OK but probably you don't have this at the beginning so you just need to add a program. 00:01:18.630 --> 00:01:21.070 You go to where brackets is installed. 00:01:21.240 --> 00:01:22.480 OK for me. 00:01:22.560 --> 00:01:23.870 In program files. 00:01:23.940 --> 00:01:27.540 Rucking said he can see the application there. 00:01:27.840 --> 00:01:31.590 Just open it and give it a name. 00:01:31.620 --> 00:01:32.670 Brackets. 00:01:33.140 --> 00:01:34.810 OK. 00:01:36.060 --> 00:01:39.890 And then you can add it to the programs. 00:01:40.130 --> 00:01:41.660 OK. 00:01:42.510 --> 00:01:50.850 OK so let's open this file using brackets. 00:01:51.030 --> 00:01:58.520 Now we can see you've got some content that is going to change the contents of this all. 00:01:58.620 --> 00:02:00.690 Let's just leave it like that so you got two lines. 00:02:00.720 --> 00:02:01.350 OK. 00:02:01.500 --> 00:02:02.340 This isn't your concern. 00:02:02.340 --> 00:02:03.910 This is some other new Coltons. 00:02:03.940 --> 00:02:04.970 OK. 00:02:05.620 --> 00:02:08.660 Well let's say this is some contents. 00:02:08.930 --> 00:02:14.110 This is some other contents only safe. 00:02:14.970 --> 00:02:18.830 OK. 00:02:19.160 --> 00:02:22.850 OK let's go back to our file handling most BHB. 00:02:23.310 --> 00:02:25.790 And now we're going to start with. 00:02:26.140 --> 00:02:34.150 If I open and close to open and close files so can I write some code. 00:02:34.230 --> 00:02:39.350 This half hour BHB tag that. 00:02:42.790 --> 00:02:43.470 OK. 00:02:43.500 --> 00:02:50.120 So first of all before we open the file We're going to open this file this textfile. 00:02:50.140 --> 00:02:52.430 Now we're going to check if the file exists. 00:02:52.650 --> 00:02:57.440 Ok so we get to say if file exists. 00:02:57.800 --> 00:03:02.450 Ok then we are going to open the file. 00:03:02.700 --> 00:03:04.030 OK. 00:03:04.140 --> 00:03:11.580 Otherwise we are just going to echo a paragraph saying that the file does not exist. 00:03:11.700 --> 00:03:14.820 So to check if the file exists there is a function we can use. 00:03:14.820 --> 00:03:16.480 Well that's it's file. 00:03:16.500 --> 00:03:19.090 Underscore exists. 00:03:19.490 --> 00:03:19.960 OK. 00:03:20.070 --> 00:03:24.020 So this function is going to take the file. 00:03:24.140 --> 00:03:24.570 OK. 00:03:24.600 --> 00:03:34.170 With the complete reference to the file name or location that is going to return a boolean true. 00:03:34.200 --> 00:03:37.530 If the file exists false if it doesn't. 00:03:37.560 --> 00:03:43.770 OK so in our case the file is in the same workspace so it is going to write the name of our file. 00:03:43.920 --> 00:03:48.710 Some text the text. 00:03:49.260 --> 00:04:02.250 Ok so if the file exists we are going to open the file and we are going to create a variable. 00:04:02.250 --> 00:04:05.960 My file is going to be of type resource. 00:04:05.970 --> 00:04:07.710 So this is a resource variable. 00:04:07.950 --> 00:04:11.690 Ok so you remember when we covered the data types. 00:04:11.730 --> 00:04:18.350 Remember it was a resource variable is so it's a reference to a file. 00:04:18.360 --> 00:04:19.370 In our case here. 00:04:19.600 --> 00:04:25.340 So then we are going to open the file using the function f open. 00:04:25.690 --> 00:04:31.320 OK so if you want to check what the function is doing just do a control K and he can see that's the 00:04:31.320 --> 00:04:35.930 file of the function is going to open a file or you are OK. 00:04:36.210 --> 00:04:43.730 And our variable is going to be to have a reference to that file. 00:04:43.730 --> 00:04:45.620 Or you are OK. 00:04:46.080 --> 00:04:52.610 So it's going to take a couple of arguments so the first one is going to be the final. 00:04:52.950 --> 00:05:00.220 So he's going to write the name of the file since we are in the same workspace and the second promiser 00:05:01.230 --> 00:05:05.740 is the permissions we want. 00:05:05.850 --> 00:05:09.750 In our case let's say we want only to read the file. 00:05:10.680 --> 00:05:20.160 Ok if you do control k again you will see that's the second problem we say is the mode and now we are 00:05:20.430 --> 00:05:23.330 in reading mode for reading. 00:05:24.030 --> 00:05:28.910 OK so and then after that we are just going to close the file 00:05:33.270 --> 00:05:41.450 to close the file we are going to close the resource itself. 00:05:44.670 --> 00:05:53.500 Otherwise we are going to echo a paragraph saying that the file doesn't exist. 00:05:54.360 --> 00:05:55.170 This file 00:05:57.810 --> 00:06:03.280 does not exist. 00:06:03.540 --> 00:06:10.200 So if we refresh the page OK we don't see anything there because we have opened and close the file but 00:06:10.290 --> 00:06:18.400 if we choose another file that doesn't exist then we'll see that this file does not exist. 00:06:18.890 --> 00:06:23.650 OK now we're going to see how to read the file using the freed function. 00:06:24.120 --> 00:06:28.450 Let's have our BHP tag first. 00:06:29.520 --> 00:06:37.020 And now we are going to open our file and place the reference to the file inside a resource variable 00:06:37.020 --> 00:06:37.170 . 00:06:37.170 --> 00:06:40.730 We going to call file handle. 00:06:41.860 --> 00:06:42.350 OK. 00:06:42.570 --> 00:06:43.300 So. 00:06:43.650 --> 00:06:50.670 So inside this variable we're going to store the output of the function f open so we get to open the 00:06:50.670 --> 00:06:51.440 file. 00:06:51.600 --> 00:06:53.420 Some text that text. 00:06:53.970 --> 00:06:57.190 And we're going to open the file in reading mode. 00:06:57.740 --> 00:06:58.610 OK. 00:06:59.580 --> 00:07:05.790 So you will notice that we haven't checked if the file exists first but what we're going to do we're 00:07:05.790 --> 00:07:16.470 going to say open the file and if it doesn't work or are you going to use a function by OK which takes 00:07:16.770 --> 00:07:27.420 a string as a barometer in a place inside the string and able to open a file. 00:07:27.600 --> 00:07:32.490 So what's happening here is we're going to try to open the file and if something goes wrong or we can't 00:07:32.550 --> 00:07:38.140 open the file then we're going to execute this function. 00:07:38.430 --> 00:07:38.910 OK. 00:07:39.030 --> 00:07:44.610 This function is the equivalence of the exits a function which is which will just stop the whole scripts 00:07:44.620 --> 00:07:44.700 . 00:07:44.780 --> 00:07:52.090 OK so then this is going to be displayed on our screen if we don't manage to open the file. 00:07:52.300 --> 00:07:53.460 OK. 00:07:54.550 --> 00:08:04.950 OK so next we aren't going to create another variable we're going to call file contents which is going 00:08:04.950 --> 00:08:07.680 to store the contents of the file. 00:08:07.930 --> 00:08:08.460 OK. 00:08:08.540 --> 00:08:09.840 And to store the. 00:08:09.850 --> 00:08:14.620 To get the contents of the file We're going to use the function f Prete. 00:08:14.680 --> 00:08:21.010 All right so let's click on control K and see what this function is doing. 00:08:21.060 --> 00:08:25.370 So it's going to return the read string. 00:08:25.550 --> 00:08:25.980 OK. 00:08:26.010 --> 00:08:26.730 Or false. 00:08:26.760 --> 00:08:32.750 If there is a failure so it's going to take a resource. 00:08:33.090 --> 00:08:37.160 OK so we're going to give it's a file handle in the first part. 00:08:37.470 --> 00:08:39.930 And the second barometer is going to be the length. 00:08:40.020 --> 00:08:40.490 OK. 00:08:40.530 --> 00:08:41.700 Going to be an intruder. 00:08:41.970 --> 00:08:46.350 And it is going to be saying how many or all the limits in bytes. 00:08:46.350 --> 00:08:55.950 OK so let's play the arguments inside the read function. 00:08:56.040 --> 00:09:02.190 So the first one is going to be our resource file file handle. 00:09:03.420 --> 00:09:04.930 OK. 00:09:05.370 --> 00:09:09.510 The second problem is going to be how many bytes you want to read. 00:09:09.690 --> 00:09:15.870 And if you want to read the whole file We're just going to place the size of the file in to get Bezuidenhout 00:09:15.990 --> 00:09:28.230 writes file size which is a function that will take as a parameter the file location of file name. 00:09:28.230 --> 00:09:32.660 OK so it is going to be the total size of the file. 00:09:32.940 --> 00:09:35.910 Now we're going to read the whole file. 00:09:35.960 --> 00:09:36.570 OK. 00:09:36.940 --> 00:09:44.730 And finally we aren't going to close the file by closing the resource. 00:09:44.880 --> 00:09:48.870 OK. 00:09:52.140 --> 00:09:52.740 All right 00:09:53.570 --> 00:10:00.940 . 00:10:00.960 --> 00:10:02.460 And let's see 00:10:05.010 --> 00:10:12.030 the contents of the file using the VAR dumb function. 00:10:12.030 --> 00:10:17.490 So you get to dump the variable file contents. 00:10:18.700 --> 00:10:19.710 OK. 00:10:22.710 --> 00:10:32.070 So if we save and refresh we can see the contents of our file so the file Constance is a string of 49 00:10:32.090 --> 00:10:38.160 characters and we can see there the contents of our file. 00:10:38.290 --> 00:10:39.130 OK. 00:10:39.130 --> 00:10:46.550 So just notice that we have a break in our file there but it doesn't show in there so it just returns 00:10:47.150 --> 00:10:50.220 the contents as a full string. 00:10:50.450 --> 00:10:54.790 So you just replace the line break with a space. 00:10:54.980 --> 00:10:56.170 OK. 00:10:57.440 --> 00:11:04.040 So that's what's the freed function is going to return. 00:11:04.460 --> 00:11:05.650 If you use it. 00:11:06.170 --> 00:11:07.310 OK. 00:11:08.790 --> 00:11:09.070 OK. 00:11:09.080 --> 00:11:17.570 So now we're going to move to the function file gets contents which will also get contents of the file 00:11:17.890 --> 00:11:18.240 . 00:11:18.320 --> 00:11:26.810 But unlike the read function we are going to read the content of the file without having to open the 00:11:26.810 --> 00:11:27.340 file. 00:11:27.500 --> 00:11:36.470 OK so we're going to go straight and use the function without opening the file and also this function 00:11:36.470 --> 00:11:40.050 is going to read the whole contents of our file. 00:11:40.400 --> 00:11:52.480 OK so let's write some code under the header so it's of or BHB tag first. 00:11:52.730 --> 00:11:54.750 OK. 00:11:55.850 --> 00:12:01.960 Then this creates a variable file contents. 00:12:02.760 --> 00:12:04.370 We already have that's very forward. 00:12:04.430 --> 00:12:12.380 So we're just going to reset the value of the variable to a file get contents through using this function 00:12:12.390 --> 00:12:12.790 . 00:12:13.190 --> 00:12:13.700 OK. 00:12:13.880 --> 00:12:20.030 So if you actually can control k you can see that the function is going to read the entire file into 00:12:20.030 --> 00:12:20.960 a string. 00:12:21.290 --> 00:12:27.600 Ok so it's going to take us 8 Rhymester filename or the file path. 00:12:27.650 --> 00:12:35.000 So it's some text does text. 00:12:35.580 --> 00:12:50.210 OK and if we can't read it then we're going to say or die unable to read file for example. 00:12:50.770 --> 00:12:52.420 OK. 00:12:53.600 --> 00:12:55.700 All right so let's have a 00:13:00.830 --> 00:13:08.990 let's have a line break or we don't the library really let's just dump the variable and see what's it's 00:13:08.990 --> 00:13:09.710 called. 00:13:10.100 --> 00:13:15.520 So a variable is file contents. 00:13:15.760 --> 00:13:16.450 OK. 00:13:16.760 --> 00:13:18.650 And then if we save and refresh 00:13:21.650 --> 00:13:24.410 we can see that it returns a string. 00:13:24.530 --> 00:13:25.400 OK. 00:13:25.490 --> 00:13:29.820 And you can see here the whole contents of our file as a string. 00:13:29.930 --> 00:13:31.940 Just like the free function. 00:13:32.060 --> 00:13:35.810 Ok but again the advantage is we didn't have to open the file. 00:13:35.900 --> 00:13:36.420 OK. 00:13:36.530 --> 00:13:44.300 And also the function will get the whole contents of the file straight away unlike the read function 00:13:44.690 --> 00:13:49.860 which will take a couple of parameters. 00:13:50.000 --> 00:13:52.200 The file is a resource. 00:13:52.250 --> 00:13:58.720 OK after opening the file and the size does we want to read OK. 00:14:00.250 --> 00:14:00.890 OK. 00:14:01.100 --> 00:14:05.100 Now we're going to use another function to read the contents of our file. 00:14:05.120 --> 00:14:07.040 It's the final function. 00:14:07.340 --> 00:14:08.100 OK. 00:14:08.100 --> 00:14:12.910 So let's try it and see what's the read. 00:14:12.980 --> 00:14:25.640 The output is going to be so let's have our PSP tag and let's say it's the file contents variable 00:14:28.490 --> 00:14:32.060 to the outside of this function file. 00:14:32.090 --> 00:14:38.510 Ok so if you click on control k you can explore details about this function so it says here it says 00:14:38.750 --> 00:14:41.780 it reads entire file into an array. 00:14:41.950 --> 00:14:47.820 OK so unlike the file gets contents which returns a string. 00:14:48.260 --> 00:14:50.860 This function is going to return an array. 00:14:50.940 --> 00:15:00.200 OK so each line of our file is going to be an element inside the array that's going to be returned by 00:15:00.260 --> 00:15:01.170 the file function. 00:15:01.310 --> 00:15:04.100 OK so let's try it and see. 00:15:04.130 --> 00:15:12.720 So the file function is going to take I was into the name of the file or the path of the file. 00:15:12.980 --> 00:15:15.860 So some text the text. 00:15:16.130 --> 00:15:16.580 OK. 00:15:16.580 --> 00:15:18.700 I'm also going to say or die 00:15:22.580 --> 00:15:26.150 unable to read the file. 00:15:26.420 --> 00:15:28.350 OK. 00:15:30.440 --> 00:15:35.110 All right let's have a new way. 00:15:35.140 --> 00:15:37.670 Break the Garret's. 00:15:37.670 --> 00:15:38.870 Dump the variable 00:15:43.250 --> 00:15:47.940 and see what it's called. 00:15:50.540 --> 00:15:57.050 If we save and refresh so we can see that it return an array. 00:15:57.080 --> 00:15:59.370 So you've got an array of two elements. 00:15:59.540 --> 00:16:02.880 So the first one is the first line. 00:16:03.590 --> 00:16:04.660 OK. 00:16:04.850 --> 00:16:07.280 The second element is the second line. 00:16:07.690 --> 00:16:09.190 OK. 00:16:09.200 --> 00:16:11.130 So. 00:16:11.630 --> 00:16:20.990 So now if you want to process the file data after using a file function then it would need to iterate 00:16:21.470 --> 00:16:27.500 over the the array and we're going to use the for each loop. 00:16:27.510 --> 00:16:31.800 OK so let's do that. 00:16:32.210 --> 00:16:36.000 So we're going to use for each. 00:16:37.030 --> 00:16:37.730 OK. 00:16:38.030 --> 00:16:41.240 So it's a numeric array. 00:16:41.240 --> 00:16:41.900 All right. 00:16:42.110 --> 00:16:44.560 So you going to place the name of the right. 00:16:44.720 --> 00:16:46.300 Which is file contents. 00:16:47.060 --> 00:16:54.200 And as a last name our variable line because each element there is one line inside our file. 00:16:54.320 --> 00:16:55.090 OK. 00:16:55.490 --> 00:17:03.780 So as a line we are going to echo the line. 00:17:03.970 --> 00:17:04.310 OK. 00:17:04.310 --> 00:17:07.960 Followed by a line break. 00:17:08.190 --> 00:17:09.870 OK. 00:17:10.820 --> 00:17:13.950 So if we try that's fresh. 00:17:14.650 --> 00:17:15.170 Here we go. 00:17:15.170 --> 00:17:17.610 So you got the first line and the second line. 00:17:17.720 --> 00:17:19.070 Let's have a live pictures before 00:17:19.080 --> 00:17:25.920 . 00:17:32.060 --> 00:17:38.650 Otherwise we can also just preens line break forward by line rather than line 4 followed by line break 00:17:38.660 --> 00:17:38.880 . 00:17:38.900 --> 00:17:41.190 It would work as well exactly the same. 00:17:41.440 --> 00:17:41.730 OK. 00:17:41.750 --> 00:17:45.710 So you can see there are elements. 00:17:45.860 --> 00:17:50.850 All right so that's how we can use the file action and that's what it returns for us. 00:17:50.870 --> 00:17:55.760 OK.