 Now that we understand some basic concepts like how the file system works, encoding, compression, things of that nature, we can do something extremely cool and very handy. We can do something called serialization. Serialization is taking an object, writing it down to disk, and then reading it back at a later time in its current state. It's extremely cool. So let's go ahead and play around with us. Say, queue to bug, queue data stream, and let's include queue file. First thing we want is some function that's going to let us save a file. We're just going to go ahead and open this file. We want to write only on this file. And if we couldn't, we're just going to exit out of here real world application. You'd want to return like an error string or something to let us know what happened. Let's make our data stream, give it a reference to our file. And one thing that we really want to do here is we want to set the version. We want to do this simply because we want to say we're using a specific version that way if cute upgrades and they change the underlying code for the serialization on the queue data stream. Well, we can revert back to a specific version so it won't break our code. And then after we've done our due diligence and our testing, we can actually upgrade to the newer version. Probably help if I did that right. There we go. You can see there's a lot of versions starting with cute one all the way down to our current 511. Don't worry if you have a newer version, this should stand the test of time. All right, so we're going to just use the newest version. Now let's go ahead and do a queue string. Let's call this title. If you've read Hitchhiker's Guide to the Galaxy, you understand that. If not, I recommend you go read the book. We're just going to make a queue int 64, not 42. Well, that'd be interesting. So in the book, they build this giant computer and they say, hey, what's the meaning to life and everything? And after centuries of numbers crunching, the computer comes back and says the answer is 42 and everybody just says, like, what? Really, really great. Now we're going to shift that information out into the data stream, which is going to put it out into the file. So really what we're doing is we're serializing two objects. We're serializing an integer. We're also serializing a queue string. We're then just going to say file flush. Really don't need it, but I'm including it for some legacy things. And then file.close. I think this operator overload and queue data stream may actually call flush for us under the hood. I just really don't want to risk it. So we're going to return true. All right. Now we have successfully written our code for our file save. Let's go ahead and save a file here. If save file, and let's just call this test.txt, then we're going to do something else. In this case, we're going to read it back. So let's go ahead and save and run and see what this thing looks like. So it just says saved. Let's go ahead and crack open our directory here. And sure enough, we have a test.txt. Let's bring this out and see what it looks like. So this is actually what the file looks like. You can see it does not have the words, and it doesn't have the number. It's got this stuff. 0, 0, 0, something t, something h, e, blah, blah, blah, blah, blah. This is where the serialized file looks like. It's not expected that we know how to parse or read this. This is all going to happen automatically for us under the hood. So let's go ahead and write our code to read this back. It seems it moved my notes onto the screen. Let me move those back off. There we go. All right. Now let's go ahead and do our Q file. If we cannot read this file back, then we're just going to return false. Once again, real world, you should at least print out an error string. I'm going to do a Q data stream. Call this in, give it a reference to our file. Now we're going to check the version. Remember that ever important version number that we put out there. So we're going to say if in dot version, and we can check for our specific version. You may be wondering, what could we do? If it was a different version, well, we would just simply read the version in. So you could say version, read the information in, resave it as the newer version. In this case, we're just using it as kind of like a checkpoint to make sure that we've got everything that we expect. And there's no little gremlins in our code waiting to pop up at a moment's notice because someone at Q changed some code on us. There we go. Got that. And we're just going to go ahead and say Q int 64. And we're just going to read those back in. And let's go ahead and return true, because we've successfully read that back out. Let's go ahead and get rid of our little comment here. All right. Let's actually just move this into a Q string, just for the sake of argument, just trying to simplify this as much as we can. There we go. Now, you may be wondering why I didn't do the, and let me just grab something off the clipboard here, something like this, where you have the similar pattern that we've been doing, Q to your current path, separator, blah, blah, blah. It's essentially the same thing, but what we're really doing is we're trusting Q to say, hey, take the current path and then just append this to it with this automatically. So sometimes you may want to trust that, sometimes you may not. Typically, I haven't run into too many issues with this. I mean, to be brutally honest, Qt is kind of amazing when it comes to that. But you get into some really weird embedded hardware and things may change. Also, something may change in the operating system. You never know. So just kind of be careful by or beware. If you feel confident, use it. If you don't, then don't. Someone will argue it will never change. It's how it is under the hood, blah, blah, blah. But there are no guarantees in life. So let's go ahead and save it, run. And you can see saved, the answer is 42 and then 42. So what we're really doing is we're saving the title and the number to the disk. We're then reading back the title and the number. Let's go ahead and swap the order and see what happens here. Just out of curiosity, see if this crashes our program. And sure enough, this is one of the gotchas about serialization. As you can see, we have a blank string and a really, really bizarre number that we have no idea where that came from. Really what it's doing is it's looking for a special order, meaning we wrote the Q string first, we wrote the integer second. So it's expecting that we're going to read the Q string first and then read the integer second. Because we reverse that order, we get, well, just garbage. So when you're working with serialization, if you start getting weird garbage, check your ordering of your variables. Let's go ahead and swap those back around and everything should just work as expected. So what is serialization really good for? Well, let's say you had a bunch of cute objects and you wanted to save them to a disk and then read them back at a later time. Like maybe the user powers off their laptop or you want to pump them across the network and read them on a completely different system, that's what serialization is really for. You don't have to mess around with any weird encoding schemes or anything like that, it just works. I hope you enjoyed this video. It's part of a video series I have out on udemy.com. This particular video is a follow-on in the intermediate course, but you can start in Qt Core for beginners. And if we just kind of crack this open, it's got a lot of content that will take you from an absolute beginner that just knows absolutely nothing from Hello World all the way up to what are templates, generics, air handling, and classes. This specific video is part of the Qt Core intermediate class. This picks right up where the beginners course left off and we do things like memory management, collections, working with settings, the file system, a lot of file system, compressions, serialization, and much, much more.