 Hey everybody, it's Brian and welcome to the, wow, the 90th Qt tutorial with C++ and GUI programming. We are just flying through these. My window may look a little bit different. Let me explain here. It's because I upgraded Qt Creator to 2.2.1. So if you have a different version, it may look a little funky. I know they kind of changed things up a little bit. And as you can see, the menus have kind of changed. Qt Quick Project, Qt Widget, and you get like a GUI application, mobile application, other project, console, and it's kind of neat how they move things around. You see how there's like an HTML5 application now, designer, widgets, empty projects, etc., etc. We're just going to stick with, for this tutorial anyways, the tried and true Qt console application. And we're going to call this a tstream for text stream. And let's just go ahead and put it in the usual location. And it says Qt Creator can set up the following targets for the project, desktop. And this is your build environment basically. You can go ahead and customize this pretty much any way you want to. And then version control if you use it. And just next, next, finish, finish. And same thing as always. They've just, you know, modified a few things here and there. But it's pretty much the same application. And it's, I think it's a little better in previous versions, but you know, that's just me. I'm kind of picky though. All right. So first thing we want to do is we want to add our includes. We're going to say Qt Core. And I took a poll out on the website and seems like most everybody wants more networking. And I'm not quite sure if you want more networking applications or more networking theories. So just drop me a line and let me know which one. All right. So today we are going to be covering drumroll Qt text stream. So if we go up to the help system, type up in Qt text stream and you can read all about it in here. And it says the Qt text stream class provides convenient interface for reading and writing text. And as you can see from the sample code they have in here, it's a very convenient class. And we're just going to go over a very simplistic example of reading and writing. So first thing we need to do is make our write file because we need to write out a file. And then we're going to read a file. So say void read. And in our program, we're just simply going to help if I could keep my cursor in one place. We're going to read the file and then we're going to write, I'm sorry, we're going to write the file. And then we're going to read the content. It's got a little head of myself there. So let's tackle the write code. We want Q file and we'll just call this file. And we want to give it a constructor and we want to give it a file name. Now if you remember from previous tutorials, if you've even seen them, the Q file class opens a file and closes a file and does things of that nature. So what we want to do is we want to say e or actually put it wherever you've been building these. E test and I'm just going to say my file.txt. Now you notice these forward slashes even though I'm on a Windows system. Well that is the, I think they call it the universal slash. Basically on Windows it would be like this. The problem with that is that's an escape character. It would be expecting something slash r slash n. So Qt has done kind of the ingenious thing of saying okay use a forward slash and it will work on Windows or Linux. So obviously you wouldn't have an e drive in Linux but it would be something like that. Simple enough but you know it warranted a little explanation. And then we're just going to say if file and we want to open. Now because we're opening a file we need to tell it how we want to open the file. And to do that we say Q IO device because you know Q file is a descendant of Q IO device and we want write only because we're going to write to the file. And then we want to say or Q IO device and we want text. Now some of you may be wondering what does that do? Well what we're saying here is we're saying we want write only access and we're oring. That's what that little bracket here means. We're oring text mode. Some of you may remember like the old days the FTP I still do this where you have to specify whether it is a text file or a binary file. What we're writing a text file and the difference between the two is the way Qt will process the information. It will process specific characters a specific way. I know it's kind of a complex topic but just know that if you're writing text Qt is going to do some processing on it. So what we're saying is if the file is open in write mode then do something. The first thing I always do is I make sure we close that file because we've already opened it. And then I make sure we flush the contents. Flush is kind of an interesting topic. It's kind of like flushing a toilet. Think of this file object as well unfortunately a toilet and you're just going to throw stuff into it and then when you're done you're going to flush it down to the hard drive. I know bad analogy but it makes people snicker every time. But the reason why you do that is because you want to close the file that way it doesn't remain open. You want to flush it that way all the contents are written to this. Now one would argue you don't really need to call flush because it's called automatically by most classes but better play it safe than sorry. Give ourselves a little notification here what's going on. We'll just say file written. Now comes into play Qtxtream. And what Qtxtream does is it actually creates a stream. What is a stream you ask? Well I'm glad you ask. I know I've been watching Mythbusters way too much. Anyways a stream is well like a stream like a river. It's just a steady stream of information. Stream doesn't really care about a beginning or an end it just dumps information into it and it goes. So we use this stream object to stream data into the file. And we're going to say hello and then we're going to add some return. And let's just say world. We're just going to do a simple hello world. I know it's been over done millions and millions of times but there you go. So that is how we are writing our file. Let's break this down really quickly. We're creating a Q file object. We're giving it a file name. We're saying if file.open meaning if we can open the file using write only mode and text. I should say using write only and text mode. Then we're going to create a Qtxtream. Stream the information into the file. Flush it and close it. Now you should know that you should call it stream.flush because stream is the one writing into the file. Think of stream as a buffer. It's like a river or stream. And you're going to throw like a piece of paper into the stream and it's going to float down the stream. That's actually probably not why it's called a stream but it's the best analogy I got. So you guessed it. The read code is actually very very similar. Give it a file name. Say file.open and we want to change the mode because we don't want to write. We want to read only. Still keep it in text mode. We want to make a Qtxtream once again and notice how we're giving this a reference to the file. And then what we want to do here is slightly different. We don't want to flush because well we're not writing anything. It doesn't hurt to do that but you don't really need it. But what we want to do is we want to read every single line in that file. Granted we know the context of it already but let's say we're reading somebody else's file. We don't really know what's in there. So let's go Q string and we'll call this line. So we're going to make a variable called line and we're going to read each line out of here. So we're going to say do. And we want to do while not line is null. Meaning what we're going to do is we're just going to keep reading this and we're going to keep reading the file line by line until we get a null line. Meaning there's nothing there. So I'll say line equal and you guessed it. Stream. Read line. And you notice how it says Qn64 MaxLine. Well what we're saying if we were to specify that is the maximum length of the line. We're going to grab it no matter what size it is and then we're just going to print it out. And then of course once the line is null meaning we've hit the end of the file we're just going to close it and say the file's been read. So there's our application and all it's beauty. Let's try to run this and see if we have any problems. And wouldn't you know it we got a good build. So we wrote the file and we said we read the file. We wrote the file and then we're reading it hello world hello world. And you notice this blank line right here this quote unquote where's that coming from. Well if you remember we did a slash r slash n after each one of these. That's where that's coming from it's just a new line so it's a blank line. And then we hit the end of the file and it says file read. So that in a nutshell is QTECH stream and Q file and how to read and write files. I should warn you that in the real world reading a file typically indicates you want to do something with it. So I know I'm going to get a bunch of messages saying well Brian what about this file. How do I read this how do I read that format. Well that gets into document object models and file formatting. And that's something we're probably not going to touch too much on because there's a hundred trillion trillion different types of files out there we couldn't possibly cover them all. But we are going to cover XML next because that's a very popular file type. Anyways this is Brian thank you for watching. And I know I push it quite a bit but go ahead and visit my website go to tutorials. Sort by Q and then you can find all the tutorials and the source code out on my website. One other thing I wanted to mention is a lot of these tutorials have been driven by your guys's user feedback. And I'm really thinking about plucking down the cache and getting the QTECH advanced advanced advanced books and just plowing through those and seeing and then they cover like a lot of the rich text and document object models and things like that. And I've got a copy of the advanced book but there's a couple other books out there I kind of wanted to buy too. So let me know if you guys have any other book recommendations. I know some of you people out there watching these videos are actually gurus in yourself because you've kind of pointed me in the right direction when I was wrong. So if you know of any good books let me know. Anyways I'm battling this Brian thanks for watching.