MyProgrammingLab12-1 Part 1 Transcript

Print

All right, let me help you with 12.1. So go to MyProgrammingLab, and log in. And there we're going to hit the plus sign. Not the arrow. The plus sign. Because remember, the arrow opens everything up completely wide open. So hit the plus sign, and then open up Week 1. And then we're going to go to Chapter 12, so hit the plus sign on Chapter 12. And there were going to hit here, 12.1. 

All right, let's click on the first one. There it says, "Given an output," so you're given the output stream; it's already created. "Associate it with the file named Year Summary." 

So the way you create it is output file stream, and then you call it whatever you want. In this case, they want us to call it output, like that. So this line has already been written for you because it says, "Given an output file stream object." So that; line's already written. Okay? 

So if that file has already been written, then how do you associate it with the file named Year Summary text by opening it for appending? All right? 

The way that you do it is just hit the eText, see right up here, it says eText, and click on that, and that opens up your textbook. And then with your textbook open, we know we're doing Chapter 12. Okay? And right there is Advanced File Operations. So I'm going to go ahead and hit the arrow on that, and that's going to open it up. And then we're also doing 12.1 because that's what the homework's based on, File Operations. And this is a really good textbook. So you definitely want to use this textbook, and read it, and follow along with it. They give you code samples, the whole works. But we're doing 12.1. So I'm going to click on 12.1. 

And then here on 12.1, File Operations. And then they talk about how it's used all over the place. And then this arrow right here takes you to the next page. And then they talk about the input file stream, the output stream, and then the stream. 

So the input is for reading files because it's from the program's perspective. So from the program's perspective, I want to input data from the file into the program. When you're writing data from the program, you're going to send data out of the program over to the file. So that's output. So it's from the program's perspective. It's input into the file, output out to the file. File stream let's you actually read and write at the same time, which is very interesting.

So I'm looking at this, and right there is iOS app. This is the append mode. So if the file already exists, its contents are preserved, and all output is written to the end of the file. And, "By default, this flag causes a file to be created if it does not already exist." 

And then, out mode is another one that we use quite a lot, and that is output mode. "Data will be written to the file. By default, the file's content will be deleted if it already exists." So the content of the file gets wiped out if you're using output mode. 

So this right here is how you associate the file. And this is for reading, because that's input mode, right? And that's in, so that's for reading a file. To write to a file, you want to use iOS out, like this, but you use the open command. And there's our answer. 

So I flip back over here to MyProgrammingLab, over here, and this line has already been written, because it says, given. So all I have to do is simply say, "output dot open", and then the file name is "Year Summary dot txt". And then, we want to do it for appending, so that's "iOS colon colon app", like that. All right? So this line we get rid of, because that was given, and that's the line to open it up, so you can start writing to the file in the append mode. 

Let's go to the next one. "Given an output stream object called output, associate it with the file named Year Summary text for output in a way that truncates any existing data from the file." So that it wipes out any existing data. 

Well, we just read that. So I'm going to say, "output dot open". And the file name is "Year Summary dot txt". And "iOS colon colon out", because that's the one that truncates. All right, so I'm going to hit Submit. 

Now, check this out. Here's something that you need to know. If you don't tell it how to open, so if I get rid of this right here, it opens up by output by default. So it will wipe out your data by default. 

And the reason that that's true is because we normally write to temporary files. And then, once the data has been written successfully, then we rename the file to the old file name. So that way, if we have a power outage, or if we have a disk error, or something like this, the most that you will lose is just the very latest change, and that's it. So that's why the default is writing to temporary files, and if you write to a temporary file, and if data already exist there, you want it gone. So you want to purge the data, and then write the good stuff to it. So that's why by default will purge the data from the file. So it will use the "out" function by default. All right? So, I'm going to leave this off, and watch this. Boom. It still works because the default is "out". 

All right, you have a file that's already given. It's already been created. And you want to truncate any existing data. So it's basically like the last one. So I'm going to say, "menu dot open". And it is "Today's M dot txt". So the menu is going to change every day, so we're going to write it, and we want the old data gone. So "iOS colon colon out", like that. 

I'm going to go to the next one. And, "Given the file stream called input, associate it with host data by opening the file for input." So I'm going to say, "input file stream". And actually it's already been created. I was just about to create it, but it says right here, "given." So it's already been created. So I'm going to go ahead and say, "input dot open", and the file name is "Host Data", and "iOS colon colon in", like that. iOS colon colon in. And that's to read, so this is for input. So we're going to be reading the file. So I hit Submit, and it works. 

Now also keep in mind that the input, if you leave it off, the "in" is a default. So I can actually just leave that off, and then just say "input dot open", and then give it the file name, and that's it. Call it done. All right? So that works, as well. 

I'm going to pop over here. "Given a boolean variable called file exists, write the necessary code to set this variable to true if the file exists, and otherwise, set it to false." So what we're going to do is just open it up. So I'm going to go ahead and say, "input file u". And the way you test for file to see if the file exists is you open it up to read. 

Now what if I didn't remember that? What if I didn't know that? There's two different things I could have done. The first is I could hit the textbook. And, again, it's a good textbook. And here I opened it up twice. So let me go ahead and shut down the second copy. So I'm going to go textbook here, and then with that textbook, let me go back over to the front, and you can see File Operations, File Output, no, we're File Operations, because we're definitely doing 12.1 right now. So I'm going to go ahead and click on that, and see what that does for me. 

So that takes me in. It does not take me in section by section. So I'm going to go ahead and hit the next page. And then this is using the file stream. Cool. I'm going to hit next page, and then next page. 

And I have it cranked up pretty high. Let me go ahead and kick it down like this, so I can see the whole page. See that? So I've got a whole page. All right, so I'm going to make it so I can see the whole page. So now I hit Next, and then, File Open. 

So you can see what it's talking about here. Default mode. So right there it talks about the default modes I have mentioned. I hit Next, and then "Checking for a file's existence before opening it." Oh wow, that looks perfect, doesn't it? So I'm going to go ahead and crank it up so I can read it better.

[End of Audio]