In this video, we'll go over reading and
writing files, the advantages of buffered
I/O and how to take advantage of traits.
To be able to read and write files we're
going to need to import standard file
system file. Now let's go down to the
point in the file where in file and out
file have already been figured out, I'm
gonna take advantage of the read trait
to make a single variable that we can
replace I/O standard in with, I'll call it
reader, it needs to be mutable so it can
keep track of its internal state, and
we're gonna make it a box dyn read. Box
is a smart pointer with a fixed size,
which places its value on the heap. In
this case the value is anything that
satisfies the read trait, and it just so
happens that both standard in and files
satisfy the read trait. So if I have an
in file, I'll open the file and put it in
a box, and return that. I'll use question
mark for error handling, because if we
have any problems we just want to exit
main with the error. Otherwise we're
gonna box up our standard in, we put our
semicolon on the end because we're using
our if expression for an assignment. Now
it can replace our standard in with
reader. Let's go down and do the same
thing with standard out. Let's create a
variable called writer this time, and
we'll make it a box dyn right, and if
our out file is not empty, then we'll box
up
file, this time we'll create the file out
file, use our question mark for our error
handling again, and otherwise we'll go
box up a new standard out, and take that.
Now we need to go down and replace I/O
standard out with writer. Let's try this
out, let's start by compiling and running
this the same way that we have with
standard in and standard out. Make sure
nothing is broken, ok it's working we get
our our dual output there, because of our
in progress. If we reroute our output to
dev null, then we only see the progress
and overwrites itself, so that's all
working like we expect. So what if we
change that to out file dev null, which
is a file, works, okay. So what if we do a
file on the file system not a special
device file,
so this routed it to hello text, let's
see if it's here it's there it's 6 bytes,
that's good let's cat it out just make
sure it actually wrote the contents,
hello, okay. So we're out files all
working that's fantastic, let's go take a
look at the in file side of things.
Instead of echoing hello and piping it
to our program, let's read it out of the
hello text file and see how that works,
will still route the output to the
device file dev null, okay, great,
six bytes it's still working even when
we read in from a file. What if we read
in from a file and do silent, okay now we
see the output and not our progress,
perfect. We can also change that to the
long form of silent but we're really not
testing arguments so let's not do much
of that. Now let's try piping yes to our
program to get a whole bunch of input
and write it out to a file yes dot txt,
good seems to be working, let's ctrl C
and take a look at our file.
Compare the sizes, same size, that's a
good sign, the file exists.
Okay, now let's turn this around let's
use yes text as a large input file, and
then let's output to dev null, we'll just
read it it and count it great. So we can
tell that it's reading okay, it's writing
okay, it's counting, it's functioning
properly,
but it's not functioning as fast as it
could. File does not buffer its output so
let's go add a buff reader and a buff
writer. These struts will take anything
that implement the read or write traits
respectively, and wrap them and provide
buffered behavior. Another nice thing
about using the trait system. So we can
come down here and wrap our file reader
in buff reader, and then we can wrap our
file writer and buff writer, and we get
buffering for free. Now buff reader and
buff writer also implement read and
write, so we don't have to change any of
the rest of our code. To see if our
buffered reader and writers work
correctly, let's read from yes text and
write out - yes - text, went nice and
quick, let's check to make sure that the
files look the same, they do great, we
have buffered file I/O. But wait buff
reader and buff writer can wrap anything
with the read and write traipse,
including standard in and standard out,
so let's have some buffered standard in
and some buffered standard out as well.
Wrap them just the same,
let's cat yes - and pipe that into our
program, and then redirect that out - yes
three detects. So this is all pipe
standard and standard out, also nice and
quick, yes, check the files, looks
functioning correctly. In the next
section, we'll have a look at applying
multithreading features to your project.