1 00:00:00,410 --> 00:00:03,469 In this video, we'll go over reading and 2 00:00:03,469 --> 00:00:06,020 writing files, the advantages of buffered 3 00:00:06,020 --> 00:00:08,710 I/O and how to take advantage of traits. 4 00:00:08,710 --> 00:00:12,260 To be able to read and write files we're 5 00:00:12,260 --> 00:00:14,480 going to need to import standard file 6 00:00:14,480 --> 00:00:17,659 system file. Now let's go down to the 7 00:00:17,659 --> 00:00:19,789 point in the file where in file and out 8 00:00:19,789 --> 00:00:23,599 file have already been figured out, I'm 9 00:00:23,599 --> 00:00:25,609 gonna take advantage of the read trait 10 00:00:25,609 --> 00:00:28,069 to make a single variable that we can 11 00:00:28,069 --> 00:00:31,009 replace I/O standard in with, I'll call it 12 00:00:31,009 --> 00:00:34,400 reader, it needs to be mutable so it can 13 00:00:34,400 --> 00:00:36,380 keep track of its internal state, and 14 00:00:36,380 --> 00:00:41,240 we're gonna make it a box dyn read. Box 15 00:00:41,240 --> 00:00:43,550 is a smart pointer with a fixed size, 16 00:00:43,550 --> 00:00:46,460 which places its value on the heap. In 17 00:00:46,460 --> 00:00:49,070 this case the value is anything that 18 00:00:49,070 --> 00:00:51,950 satisfies the read trait, and it just so 19 00:00:51,950 --> 00:00:55,540 happens that both standard in and files 20 00:00:55,540 --> 00:00:58,820 satisfy the read trait. So if I have an 21 00:00:58,820 --> 00:01:01,670 in file, I'll open the file and put it in 22 00:01:01,670 --> 00:01:04,608 a box, and return that. I'll use question 23 00:01:04,608 --> 00:01:06,289 mark for error handling, because if we 24 00:01:06,289 --> 00:01:08,210 have any problems we just want to exit 25 00:01:08,210 --> 00:01:10,729 main with the error. Otherwise we're 26 00:01:10,729 --> 00:01:13,759 gonna box up our standard in, we put our 27 00:01:13,759 --> 00:01:15,560 semicolon on the end because we're using 28 00:01:15,560 --> 00:01:17,509 our if expression for an assignment. Now 29 00:01:17,509 --> 00:01:19,280 it can replace our standard in with 30 00:01:19,280 --> 00:01:22,579 reader. Let's go down and do the same 31 00:01:22,579 --> 00:01:25,670 thing with standard out. Let's create a 32 00:01:25,670 --> 00:01:28,249 variable called writer this time, and 33 00:01:28,249 --> 00:01:32,659 we'll make it a box dyn right, and if 34 00:01:32,659 --> 00:01:37,729 our out file is not empty, then we'll box 35 00:01:37,729 --> 00:01:39,200 up 36 00:01:39,200 --> 00:01:42,409 file, this time we'll create the file out 37 00:01:42,409 --> 00:01:45,290 file, use our question mark for our error 38 00:01:45,290 --> 00:01:49,130 handling again, and otherwise we'll go 39 00:01:49,130 --> 00:01:52,630 box up a new standard out, and take that. 40 00:01:52,630 --> 00:01:55,640 Now we need to go down and replace I/O 41 00:01:55,640 --> 00:01:58,880 standard out with writer. Let's try this 42 00:01:58,880 --> 00:02:02,119 out, let's start by compiling and running 43 00:02:02,119 --> 00:02:04,490 this the same way that we have with 44 00:02:04,490 --> 00:02:06,950 standard in and standard out. Make sure 45 00:02:06,950 --> 00:02:09,709 nothing is broken, ok it's working we get 46 00:02:09,709 --> 00:02:11,870 our our dual output there, because of our 47 00:02:11,870 --> 00:02:15,380 in progress. If we reroute our output to 48 00:02:15,380 --> 00:02:16,970 dev null, then we only see the progress 49 00:02:16,970 --> 00:02:19,160 and overwrites itself, so that's all 50 00:02:19,160 --> 00:02:21,709 working like we expect. So what if we 51 00:02:21,709 --> 00:02:24,019 change that to out file dev null, which 52 00:02:24,019 --> 00:02:28,940 is a file, works, okay. So what if we do a 53 00:02:28,940 --> 00:02:30,860 file on the file system not a special 54 00:02:30,860 --> 00:02:31,670 device file, 55 00:02:31,670 --> 00:02:34,580 so this routed it to hello text, let's 56 00:02:34,580 --> 00:02:37,340 see if it's here it's there it's 6 bytes, 57 00:02:37,340 --> 00:02:39,739 that's good let's cat it out just make 58 00:02:39,739 --> 00:02:41,180 sure it actually wrote the contents, 59 00:02:41,180 --> 00:02:44,120 hello, okay. So we're out files all 60 00:02:44,120 --> 00:02:47,570 working that's fantastic, let's go take a 61 00:02:47,570 --> 00:02:51,160 look at the in file side of things. 62 00:02:51,160 --> 00:02:54,380 Instead of echoing hello and piping it 63 00:02:54,380 --> 00:02:56,000 to our program, let's read it out of the 64 00:02:56,000 --> 00:02:58,430 hello text file and see how that works, 65 00:02:58,430 --> 00:03:00,410 will still route the output to the 66 00:03:00,410 --> 00:03:03,170 device file dev null, okay, great, 67 00:03:03,170 --> 00:03:05,660 six bytes it's still working even when 68 00:03:05,660 --> 00:03:08,570 we read in from a file. What if we read 69 00:03:08,570 --> 00:03:10,790 in from a file and do silent, okay now we 70 00:03:10,790 --> 00:03:12,500 see the output and not our progress, 71 00:03:12,500 --> 00:03:14,780 perfect. We can also change that to the 72 00:03:14,780 --> 00:03:16,549 long form of silent but we're really not 73 00:03:16,549 --> 00:03:18,560 testing arguments so let's not do much 74 00:03:18,560 --> 00:03:22,430 of that. Now let's try piping yes to our 75 00:03:22,430 --> 00:03:24,769 program to get a whole bunch of input 76 00:03:24,769 --> 00:03:27,880 and write it out to a file yes dot txt, 77 00:03:27,880 --> 00:03:30,980 good seems to be working, let's ctrl C 78 00:03:30,980 --> 00:03:34,270 and take a look at our file. 79 00:03:34,270 --> 00:03:36,280 Compare the sizes, same size, that's a 80 00:03:36,280 --> 00:03:38,350 good sign, the file exists. 81 00:03:38,350 --> 00:03:40,960 Okay, now let's turn this around let's 82 00:03:40,960 --> 00:03:44,590 use yes text as a large input file, and 83 00:03:44,590 --> 00:03:46,780 then let's output to dev null, we'll just 84 00:03:46,780 --> 00:03:49,300 read it it and count it great. So we can 85 00:03:49,300 --> 00:03:51,340 tell that it's reading okay, it's writing 86 00:03:51,340 --> 00:03:53,500 okay, it's counting, it's functioning 87 00:03:53,500 --> 00:03:54,130 properly, 88 00:03:54,130 --> 00:03:57,250 but it's not functioning as fast as it 89 00:03:57,250 --> 00:04:00,850 could. File does not buffer its output so 90 00:04:00,850 --> 00:04:03,730 let's go add a buff reader and a buff 91 00:04:03,730 --> 00:04:06,490 writer. These struts will take anything 92 00:04:06,490 --> 00:04:09,340 that implement the read or write traits 93 00:04:09,340 --> 00:04:12,580 respectively, and wrap them and provide 94 00:04:12,580 --> 00:04:15,820 buffered behavior. Another nice thing 95 00:04:15,820 --> 00:04:18,370 about using the trait system. So we can 96 00:04:18,370 --> 00:04:21,459 come down here and wrap our file reader 97 00:04:21,459 --> 00:04:24,190 in buff reader, and then we can wrap our 98 00:04:24,190 --> 00:04:27,459 file writer and buff writer, and we get 99 00:04:27,459 --> 00:04:30,580 buffering for free. Now buff reader and 100 00:04:30,580 --> 00:04:32,950 buff writer also implement read and 101 00:04:32,950 --> 00:04:34,690 write, so we don't have to change any of 102 00:04:34,690 --> 00:04:37,000 the rest of our code. To see if our 103 00:04:37,000 --> 00:04:38,680 buffered reader and writers work 104 00:04:38,680 --> 00:04:41,080 correctly, let's read from yes text and 105 00:04:41,080 --> 00:04:44,410 write out - yes - text, went nice and 106 00:04:44,410 --> 00:04:46,570 quick, let's check to make sure that the 107 00:04:46,570 --> 00:04:49,600 files look the same, they do great, we 108 00:04:49,600 --> 00:04:52,990 have buffered file I/O. But wait buff 109 00:04:52,990 --> 00:04:55,380 reader and buff writer can wrap anything 110 00:04:55,380 --> 00:04:57,910 with the read and write traipse, 111 00:04:57,910 --> 00:05:00,730 including standard in and standard out, 112 00:05:00,730 --> 00:05:03,340 so let's have some buffered standard in 113 00:05:03,340 --> 00:05:05,550 and some buffered standard out as well. 114 00:05:05,550 --> 00:05:07,750 Wrap them just the same, 115 00:05:07,750 --> 00:05:11,680 let's cat yes - and pipe that into our 116 00:05:11,680 --> 00:05:15,220 program, and then redirect that out - yes 117 00:05:15,220 --> 00:05:17,320 three detects. So this is all pipe 118 00:05:17,320 --> 00:05:19,419 standard and standard out, also nice and 119 00:05:19,419 --> 00:05:23,580 quick, yes, check the files, looks 120 00:05:23,580 --> 00:05:26,580 functioning correctly. In the next 121 00:05:26,580 --> 00:05:28,229 section, we'll have a look at applying 122 00:05:28,229 --> 00:05:33,229 multithreading features to your project.