We don't have to use just textfiles a lot of the time.
All we want is to store some data in REPAK quickly not caring about the readability of the file itself.
And binary files are the perfect complement to this.
So what I've done here a start is I've created a structure.
It has two members one in value two and it's integers and I've created a an array of this type and type
data and there is just about 10 of them here Max is 10 and it is randomly stores and values into it.
OK.
And I'm just outputting the results.
So you run this just randomly stores a bunch of Vout.
OK.
And I'm doing this because we're going to actually write out these values into a binary file and then
read them back into a different array.
So let's get started of how to write something right.
File a binary file
stream
a file
and when we opened the file
it dated been this doesn't exist yet.
Will.
And when we open it will actually say it's a binary file.
Like this.
Use extreme and colon colon binary.
OK.
And so now that we have opened it in the binary kind of format We'll write to it and we can't just use
the insertion operator like we do with Seow tour.
What we did before we actually have to call a different function OK because the insertion operator really
only works with Tex or possibly like integers just like just basic types like that.
OK so what we're going to do is call out file dot right.
And right actually we'll write code bytes essentially and we know that a character is one byte.
So we're going to do is actually cast this to be the character character pointer and we're going to
take the address of the array data
and right to take.
So this is the data that we're going to write to the file and write needs.
The length of how much how many bytes to write.
And to do that you use the size of orated
k.
So this is kind of interesting.
If you take the size of the array and this array that's kind of static.
Well it's made on the stack.
OK.
Right.
This isn't dynamics that's made on the stack.
So if you actually take a size of an array as create on the stack it'll actually give you how many bytes
it is and total including each element.
This wouldn't work if you used a dynamic array.
If you use a dynamic array it'll just be the size of the pointer.
OK so in this one his first parameter we're taking the address of the of the array.
And then we're it to be this character pointer.
So this looks a little bit complicated but really all it's saying is be that the first element and B
type character pointer.
That's that's really all this is saying.
So it's really just it's kind of making a cursor and it's pointing at the very first bite of this radiate.
So that's really all this is saying.
Because cause right only works with bytes right.
Each byte or this many bytes.
And so now we will just close it for and what we're going to do is actually read it back in right now.
But in a new array.
So a new array of Max.
So we're not going to use the same array data used for.
And we're going to make a infile.
So I have stream in file in file open loop in the same file open for binary reading.
It's a binary reading.
The last time and essentially we're going to use the same kind of signature.
But this is this time it's not going you're right as can be read in file dot read
you begin the same thing.
Like this looks exactly the same as right but now is using right.
And we just close it
and now we're going to just loop through the new array and just output what we got.
So hopefully they'll match up.
Max plus
put everything re
k.
So it's basically it I think.
So again we're just filling the array data with some random values and then rewriting it in a binary
way.
And those that we're using it doesn't matter that these are structures.
As long as it knows how big you can make or how many bytes it is.
So it knows it because it's made on the stack so it knows that the size of this data is what 8 bytes.
And there's 10 of them so it's 80 bytes right.
Right right.
So size of can actually do that calculation kind of Two-Step calculation.
OK.
So writing 80 bytes from this array to this fight then we're reading 80 bytes again into this new array.
So you run this you will see.
So it is.
So this first one is first kind of 10 values here are all of this.
So 29 to do 10 84 125 of 28 center etc..
And then now we're reading it back.
So 9:31 for 125.
Cetera.
So it's working.
So this really binary files are really good for making kind of smaller files on the disk.
And it's also really faster to write and read it usually.
And that's usually why we want to use binary files over text files because they're really fast to write
out and they take up very little space because it's all kind of nicely packed together.
And usually it's actually quite a bit easier to read in as well and really use three lines to write
it out in three lines.
Are you back in.
So as long as you can get past what's happening here in this line.
It's pretty simple to write out a binary file and then read it back and.
And we'll be using them in kind of a mini project in the next lecture.