 Let's take a closer look and let's learn how to work with streams Remember how I said the difference between a buffer and a stream as a buffer has a defined beginning and an end Whereas a stream really doesn't Not a thousand percent accurate streams Actually have a starting point. They do have an ending point, but that ending point is basically when you decide to stop reading them Let's take a closer look We're gonna include a few things here Q to bug Now the problem with a broad statement like that is Someone somewhere out there is gonna quote something they found on Google and say well, you're not accurate This is what it really is and the problem fundamentally is depending on which site you're reading You're gonna get different information. I've even found conflicting information on the same website Won't name names, but you know who you are Alright, so we've got Q to bug Q data stream. That's a new one Q file Q. Durr We're gonna also include Q file info and we're going to include Q random Generator not sure if we're gonna use all those, but let's just wing it and see what happens here So there's Q random generator and we're gonna say bull create file Similar design pattern of what we've been using that way. It's very familiar And we're gonna say Q IO device and we want write only you may be wondering why are we using? Write only and not append it's because I want to make sure that we have reliable data every single time Where if we have a pen this is gonna keep growing and growing growing every time we run this application Give some sort of feedback case. There was a problem and Return false because there was a problem All right, once we verify that we can actually open the file up first I want to make sure that we do is well, you guessed it close the file Now we got the file open. Let's go ahead and write some data to it. So we're gonna say Q data stream and Let's call this stream We're gonna give it a reference to our file. You may be wondering why are we doing this? We can write directly to the file. Well, what the Q data streams gonna do is Allow us to stream data into the file instead of writing it in blocks It's actually super handy and it's really efficient, but so we're gonna say it max thousand Q string and let's call this banner It's gonna write random numbers and we're gonna write our banner out. So let's say Q info Don't really need to do this But just so we have some sort of visual representation on the screen that we're actually gonna do something Now we're gonna say stream and we're going to shift that data into it. There we go So now we have just written out to that stream Now let's make some random numbers here for And I equals zero I minus five and we're going to increment this and let's just say I plus plus So at least five times we're going to write out a random number What a Q int 32 let's call this num and we're going to get a bounded number now if you remember from the Previous examples what we're really do here is what we're really doing here is we're calling a singleton pattern off the Q random generator Meaning we're getting the global instance of that class and Bounded meaning it's going to take a minimum or a maximum. We want the upper limit to be equal max in this case 1,000 so not super super important that you really understand all those concepts because we're going to cover them But I wanted you to really under Understand at a very Rudimentary level what we're doing there Basically, we're just generating a random number with a maximum of a thousand and we're going to write that number out Now you'll notice something right off the bat. We're writing two different data types. We're writing a string and we're writing a number Pretty cool Let's go ahead and fill some of this out This is a really good example of it doesn't matter what the file name is it matters the data you write to it whether it's text or Not text just going to add that in there so we can tell exactly what we're doing here So if create file then we're going to actually write it out Or if create file then we're going to do something which is in this case We're going to read it back. So let's go ahead and do this now in a lot of Production-based Applications, you'll see people will not break things out into actual functions like this and They'll get some really weird error messages and they can't figure out for the life of them. What's going on? That's why I kind of break things out into functions a little bit longer a little bit more coding But it's a lot safer in the long run All right, so now we've got our file open. We're gonna say Q datastream and give it a reference to our file Another thing you may notice is that we're not opening and closing the stream We're opening closing the file, but not the actual stream That's what I meant by a stream really doesn't have a beginner end. You kind of decide where you start and stop Meaning we could have read and write things to and from that file long before we open that datastream All right, so let's go ahead and make a Q string Let's call this banner so we don't confuse notice. We're not assigning that and we're gonna say stream and We're going to read from that stream Very very similar to Working with the standard input and output. It's actually by design. It's a very elegant design and I personally love it And let's say Q info And let's just Q info out that banner and let's just For the sake of typing out yet another for loop. Let's grab this and Let's get rid of that We're gonna grab a Q in 32. We're gonna call it numb notice. We're not assigning that because we're gonna read that from the stream So if you're really paying attention to what's going on here, we open the file We open the stream by giving it a reference to the file Then we're going to read in a variable. That's a Q string Then we're gonna read in five variables that are Q in 32s or integers basically Save this and let's run it fingers crossed All right, so we wrote wrote wrote wrote and then we read and you can see sure enough we got them back So this is a string we can tell by the quotes and then we've got integers That's pretty slick Let's go ahead take a look at the actual files content. See how it's a dot txt But when we look at it, let's pull this over you can see that. Oh suddenly Magically, this is not plain text And it says there was a problem there actually wasn't a problem You can click this all day long and it's looking for a character encoding It doesn't really matter because this is no longer plain text. This is a data stream Q data stream under the hood is chosen and encoding and pumped it out to the file itself Encoding is something we're gonna cover soon, but basically you just take the data and put it in a guaranteed format That's what I mean by it doesn't really matter. This is a dot txt But we don't have plain text information in it. Just understand the fundamental difference there I hope you enjoyed this video. It's part of a video series I have out on you to me calm 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 our 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 Compression serialization and much much more