All right, so it says right here, "You create the file stream, and then you open it for reading." So sometimes you want to determine a file exists before you start writing to it. All right, so we pop over here, and we open it up. And then if the data fails, so that means the file does not exist, then we're going to start writing to the file.
So we're going to create the file, and then start writing to it. Otherwise, file already exists. So we close and we says the file values already exist. So here it is right here. So they're basically telling you that you want to open the file to read. And that way you can determine whether or not it reads. Okay, so I'm going to pop over here, and I'm going to say input file stream, in the book, they just use plain old file stream, so I'll use file stream.
So file stream, and they called it "data file", and I'm going to call it "my file". You can call it whatever you want. The important part is this part. It must be file stream because that's the name of the class. So here I'm just call it my file, and I'm going to go ahead and pop it. So it is, what is the file called, "crucial data".
All right, and then I'm going to open it to read. And then I'm going to say if it exists. So I'm going to go ahead and say the boolean is called file exists. It's going to be set to my file like that. And what did they use? They said if data file fail. So, you can actually just say my file, and that right there will tell you whether or not exists. And if it does exist, then it's true.
Okay, so here I'm going to go ahead and say, let's see, my file fail. I don't like fail though because, and the reason is because if you say fail, then that means that it's going to say false. And false is going to go into file exists. So that's backwards from what we want. So we want it to only go positive if it exists. So if you just say my file like that, that actually works. So I'm going to hit Submit just to show you that actually does work.
So I'm going to pop over here. Let's go ahead and pop open Visual Studio. And I'm going to grab this code right here. I'm going to go Control-C on it. And then Visual Studio, I'm going to do a Control-V to paste the code, and then my programming lab said that boolean, so BOOL, file exists, has already been created, all right? So then right here is my file, and let's see, no converter, no, so, I'm going to go ahead and click here.
And then say, and I always use "is open". This is what I use is is open and that's a method like that. So you try to open the file, and then you say, "Was it open?" And if the answer is, "Yes," and then the true and the false. So if it's "Yes, it is open," then that means the file exists. So this will go true like that. So I'm going to go ahead and say "if the file exists, then I want to cout file exists, and out". Otherwise, I'm going to "cout", and I would say "file does not exist", like that.
All right, so when you do your file, when you create that file, then actually creating the file here. Okay, I'm calling it "my file". And then when you hit your dot, then IntelliSense pops open, right? And you can scroll up and down, and see what rings a bell. So, as I'm scrolling up and down, I would love it if there was a method called "exists".
There's method in Java and C+ and C# that just says exists. But in C++, this is how we do it. You try to read it, and then you find out if it got opened. If it was opened, then that means that the file was there, and that's how we were able to open it. So you just check and see if it exists. If it does not open, then that means that there's no file there to read, or it is not available to you. You don't have the right to read it. So that's also a problem because we're not going to be able to read it anyway. So we can't open it. If you cannot open it, you cannot read it.
All right, so as I go down, I've got a lot of different choices, and then I just look and see what hits. And then that one right there jumps out, so "is open". Okay. Look at all these different things that we can do a files. Isn't that cool? I love peek. All right, so quite a few different things.
Now in addition to that, you also have available to you Google. As a software developer, I use Google a lot. So if you have not done a command in six months, nine months, you know it's possible because you've done it in the past and on past projects, but you don't quite remember the syntax. No big deal. You just do "C++ and file exists". Boom, there it is. So click here, and then let's see, this is what the person wants, right? So this is different choices on how to do that, given to them by these experts.
Stack Overflow I love. I love Stack Overflow because you get a lot of really good answers there. I also love cplusplus.com. This one right here because a lot of very smart people are there as well. And Microsoft does a good job, so sometimes I will check this right here. In fact, let's go take a quick look.
So Boolean path file exists. That looks pretty good, but they're having you bring in a library, an extra library. I'm not happy with that because you can do it without bringing in a large library. So I'm not going to use that one, but you can see that we can Google and come up with answers. So you want to practice Googling and looking for answers that way because that's what we do in industry. It's you want to be good at that because there's a lot of nice people out there who post questions that are often asked. And they post the answers right there on Stack Overflow, cplusplus.com, quite a few other websites like that. So make sure you're really good at Googling and using Google.
All right, so we've got these options, and the one that I like the most is going to be "is open". So I'm going to try to open it to read, and then I'm going to say, "Did it open?" And then "file exists" is going to be "yes" or "no". So that's one that I would use.
All right, "Given a file named Execution Log, write the necessary code to add the line to the end of the file." So when you're creating your program, you want to do an execution log and then say things that are happening as you're running the program. So if the program fails, or if something goes wrong, or it doesn't behave the exact was you expect, you can check your execution log.
All right, so I'm going to go ahead and pop this. So we want to open it, and we want to write that. And you have the file called Execution Log. They want you to write the entire code. So I'm going to go ahead and open it for writing. So I'm going to say "output file stream". And you can call it whatever you want. I'm going to call it O File, and you can call it out2 file. You can call it anything that you want, all right? So I'm going to call it O File.
And then its "execution.log and iOS colon, colon, app". The app is append. Now you notice that there's two different ways you can do this. You can actually create the reference, and then you can say "O File dot open" like that. And then open it like that, okay. Or you can just as you're creating the reference, you can go ahead and use a constructor that takes a string and takes the opening type. All right, so, and this is what I like just because it's one line of code versus two lines of code. But in production, you'll see both ways being used.
So I'm going to go ahead and open up the pipe to the file and create the reference in one single shot. Then the string, you'll notice it's a stream, okay, and then remember our input output string is also a stream. So that means that these two are going to behave very, very similarly. So if I want, on my output stream, if I want to put this to the console, how would I do it? I just say "cout", and then I say "program, execution successful". And then "arrow, arrow, endl". That's how I would do to the console, right?
Well, reading and writing to a file is very much the same as reading and writing to the console. The only difference is instead of using my "cout", which is an I/O stream, instead of using the "cout", I'm going to use the pipe to the file, my O File. So watch this. I highlight the "cout", and I say "O File", instead. And that's the only change that I make. Everything else I leave the same, isn't that wild? So if you can read and write to the console, you can read and write to the file because they're very much the same. All right, so I hit Submit, and that's how it works.
[End of Audio]