 Everybody, this is Brian, and welcome to the 85th Qt tutorial with C++ and GUI programming. Today we're going to do something a little bit different. I've had a couple complaints that we're getting a little too advanced, and we need to step back and review a few things, so we're going to do that. I know some of you are growing up and want to see the advanced stuff. Well, it's coming, I promise. Actually, I kind of think some of the stuff we've done has been pretty advanced, but anyways, let's get going on this one. We're just going to call this Binary I.O. And put it in the usual location, wherever you've been putting these. Now, what is Binary I.O.? Well, back in the day, if you're kind of a grumpy old man like me, you remember when you had to upload and download things via FTP, you had to choose between Binary and Text Mode. Well, when you open Notepad, just type some gibberish in, that's Text Mode. Whereas like an application is Binary. So, what we're going to do is we're going to learn how to write some stuff out to a file. We've done this before, but we've done it in text. I've had a couple of people say, hey, can you show me how to save an object to a file and load it back up? That's actually called serialization. So, we're going to add a few things here. Include, and we want Q file. Include, and we want Q string. Include, and we want Q map. And last one, I promise, include, we want Q debug. Now that we've got all our includes, we can get cracking on the code. I have to apologize if you hear this dinging noise. My phone is like on its last leg. And it just goes in and out of service. And when it goes in service, it just, I mean, it gets tagged. I get like 100 emails in a shot, and it just drives me insane. So, what we're going to do here is we're going to save. So, we're going to say int myNum, and we're going to say 22. We're just, you know, picking a random number here. We're going to say Q map, and we want a Q int, and let's just say 64. Just for the sake of argument here. And a Q string, and you can hear my phone going crazy. I really need to buy a new phone. See, I bought my phone back when it was hip and cool, and now it's just old and pathetic. But darn it, I like my phone. I paid a lot of money for it, so, well, actually I'm lying. I hate my phone. I'm really, really looking forward to getting a new one, but don't tell anybody that. So, we're just going to add a few things to our map here. And if you have no idea what I'm doing, I bet you go out and watch the other tutorials. We've covered all of this stuff. But just for the sake of argument, in case you haven't seen this, a map is like a list where you can add keys to it. So, we're just adding a few things to our map here. And now we want to save it to disk. So, we're going to say Q file, and we'll call this file, and let's say eDrive. Put this pretty much wherever you want to put your files. eDrive test, and we'll say my file.txt. Don't get thrown by the .txt. We're actually going to write a binary file, but I'm going to open it up in Notepad and kind of show you the contents here. So, we're going to say if not file open. So, we want to do some basic error checking here. And we want QIO device. And we want write only, because we're only going to write to this. So, if we cannot open the file in write mode, we're just going to return. We should probably spit something out on the screen saying, hey, problem. Danger, Will Robinson. Alright. Could not open file. So, now that we've got the file open, the first thing you should always do is flush. Actually, if you're paying attention, that's the last thing you do. But I always add it first, that way I don't forget to do it. Be sure to flush the file and close it. What flush does? It's kind of like flushing a toilet. It makes sure that everything that's in the bowl goes down the pipe. I know kind of a disgusting example and I apologize, but that's the best one I had at the time. So, we want a Q data stream and we're just going to call it out. Give that a reference to file. Now that we've got a output stream, what we should do is set the version. And you do that simply by calling set version. And we want Q data stream. And we want to set this to, and notice how it's got all the previous versions. It's always a good idea to set it to the newest version. In this case, it's Q47. You do that because the streams are treated differently with each version. That's actually not true. But what I should say is that there's a very big possibility that streams are treated differently with each version. So you should make sure that the streams line up. Never cross your streams as they say in Ghostbusters. All right. Now that we've set the version, we can just simply say minum and map. So what we're doing is we're writing this out. We're actually writing two variables out to our file. We're writing minum, the number 22, and map, which is this object right here, along with its associated data. And we're going to write it in my case, eDriveTestMyFile.txt. So let's go ahead and save. Run this. And we didn't output anything, but I can guarantee you that it saved. And now if we go out to eDriveTest, you can see there's my file. You can see there's this bizarre, looks like a cryptic alien language going on here. Well, that's actually non-printable characters is what's going on here. And this is probably this first little character, which you can't see is the character 22 or the number 22. And then you've got all this stuff behind it. This is our map structure. And you notice how it's got 3, 2, 1. So just because you can see some printable characters in there, don't be fooled. This is not plain text. And we're going to prove that right now by, well, you guessed it. I'm going to load this thing up. So let's just take this and copy that. I'm a big fan of copy and paste. It saves a lot of time here. Say load, and we're going to get rid of that. And we're going to load it from disk. And instead of write, we want read only, because we only want to read the file. And then we're going to call this in. Remember to set the version that way the streams line up. We don't need to flush because we're not writing anything. But we do need to read this back in a specific order. So what we're going to do is say in, and we want minum, and then we want map. You always want to read it in in the same order you wrote it out. If you try to do map the minum, it's going to get all screwed up because it's going to try and read that first one, which is actually a int minum. So let's take this. And let's actually, just so we don't confuse you, comment out the saves. We're not doing the save at this point. Now what we want to do is we want to actually prove that we did in fact read this thing. So I say Q to bug. And we'll say minum equals and we want to know what minum is. And then let's say for each, it will say Q string. We'll just call this item and map values. And let's kind of scroll down here so you can see what's going on here. And we just want to do a Q to bug. We just want to print that item out here. So we're just going to print the item. So real quick, let's review. We've commented out save because we just, for whatever reason, we don't want to overwrite the file. We just want to prove that we're actually reading it. So we're going to run the load. So we've got our, actually let's get rid of that. We've got our uninitialized minum, our uninitialized map. We're reading it from the same file. We're doing a read only that we were not modifying the contents of the file. We're, oh, caught a bug. That would have been nasty. We're setting the version to make sure it matches up with the version that it was written in. And then we are reading in the same order that we wrote out. Closing the file and then we're just printing it out on the screen. Let's run this, see what happens. Sure enough, minum 22 and then one, two, three. So there you go. In all its glories, that's how you read and write binary. Well, this is Brian. Thanks for watching. I hope you found this educational entertaining. And once again, all the source code will be out on my website, voidrealms.com. Just go to tutorials and then cute and then whichever one you're looking for. So thanks for watching and shoot me some feedback. I like hearing from you guys.