 Hey everybody, it's Brian and this is our 12th Qt video with C++. And what we're going to do is cover the Q file. What is Q file and how do you use it? Well, Q file, if you look in the Help file, is a class that provides an interface for reading and writing files. Alright, let's get going on this. Go File, New Filer Project, Qt Application, Console Application, and we'll say Files, Next Finish, choose your framework version. Alright, and we're just going to add a reference to Q string here. And the first thing we're going to do is actually make our functions. We're going to make two functions, a read and a write. So we'll call this write, and we're going to make another one called read. And let's not forget our handy include to Q debug. Now the first thing we want to do is actually create our file name. Let's choose a file I know doesn't exist just for the sake of speed and argument here. And then we're going to call the write function to write to the file. Oops, probably help if we gave that a variable name. Boy, I'm just not doing good with that variable today, am I? So we're going to write to the file, and then we're going to read to the file. Very simple. I'm trying to keep the main function very nice and clean, very simple to understand because I really want to focus on Q file and how to use it. In our write function, we want to do Q file. We'll just say I'm file and file name. The first thing you want to do is open the file. So we say if not open, and then we have to give it some parameters here, and we want to say Q file write only, and we're going to write text. So we'll say Q file text return. And let's actually write out that we couldn't do it. So we'll say Q debug, could not open file for writing. All right, now, if we've gotten to this point, we have an open file. So the first thing you're going to want to do is make sure that you close that file. Now that you've ensured you've got an open file and you're going to close it, then you can add the real meat of the program here. And I'm going to use the magic of copy and paste here and actually include, well, I cannot type include today, Q text stream. Now, what is Q text stream? Well, if you just select it in F1, Q text stream is a class that provides convenient interface for reading and writing text. You can go ahead and read this and it gives you actual directions on how to read and write files. It's very, very good to go through the help system and learn what you're doing rather than just blindly following tutorials you find on the internet. But for the sake of argument, let's just keep trudging on through here. So, okay, text stream and we're going to call this out. We have to give it a reference to our open file. And then we will simply say out, hello world. Now, it's good practice if you're working with files to actually flush them. Now, what does flush do? It's kind of an old programming joke. It's like flushing the toilet. You're throwing data into a stream and then you want to flush it. Make sure everything gets written down to desk. Most modern operating systems, you really don't need to use the flush command, but it's a good idea just to add it anyways. All right. So, there is our write function. And we can pretty much just take this, copy and paste it right into read and we've got almost everything we need. But now, of course, we need to change a few things. We'll say read only and text and cannot open the file for reading. And, of course, we're not making it out. We're making it in. And what we're going to want to do is say a queue string and we'll just call it text. We want to do in read all. Let's do queue debug. And we're just going to write the text out. And, of course, we're not writing anything. So, flush would do absolutely nothing for us. All right. Let's review really, really quickly here. We have our write function, which takes a file name. We're making a queue file object with that reference to that file name. And we're saying if we cannot open the file for write, that's a file mode. We're going to write to that file. That's actually locking on some operating systems. What that says is you're locking that file so nobody else can modify it. And you're only going to write to it. And if you can't, we're going to just say, couldn't open it and return. Otherwise, we're going to actually create a queue text stream. And we're going to write to the file, flush the contents, and then close the file. And then when we go to read, it's almost the exact same thing except for putting data in the file. We're pulling it out. And we are going to just print the text out into queue debug. Now, if everything works, and I actually have to admit I haven't practiced this is all on the fly. So we may actually have a few errors. Let's compile and run. Well, sure enough, it worked. Hello, world. So what happened was it opened the file, wrote hello world to it, and then read it back and printed it into the console here. And let's just try running this again. Let's see. Sure enough, hello world. Pretty neat stuff. As you can see, it's not very hard to read and write a file in queue. You just have to remember some of the basics which are you create a queue file object. You open the file. If you cannot open the file, you have to immediately exit. You can't try to write anything to it. Once the file is open, be sure to close it. If you write anything to it, be sure to flush the contents. When you read the file, there are several methods to read. I'm only going to cover, excuse me, the read all. The reason for that simply is time. If you want to go into the help system and queue file, it actually has some very, very nice documentation on different ways of handling it and even colors like how to remove a file. All right. So we're running out of time here, but this is Brian. Thank you for watching. I hope you found this video educational and entertaining, and we'll see you next time.