Hello, welcome to the 71st tutorial in the c++ program series. And this program is looking at how to open a file. So this could be a text file to be an Excel 40, wherever. And we'll be looking at how to open a follow but we will show you how to also add a file to an Xcode project with Visual Studio etc. It isn't difficult and you can ask us or find the answer for Google. So first of all, what we're going to do is simply create a simple file in text documents in the oh we're gonna put it Nope.
Save it. Save it to our desktop. I'm gonna call it Hello dot txt but the RTF extension just changed that or to feel like I should be a okay as well, but just for the text file extension. So to include it first of all just want to open up your projects or not. So this is Xcode. It's different how you would do it in Visual Studio Visual Studio is a lot easier.
You can find the answer boogie board just message us both for Xcode, what you want to do is go to Xcode preferences, and you want to go to locations. And then you want to go to the derived data folder and then what you need to do to validate is click this little arrow here. And now you're at your derived data folder. And then you put your fall inside where your project folder and our project is called c++ tutorial. If we go through, it will be here in a moment. It's right here.
You want to go into here, you and go to build, products, debug and hear us place anything that you want to include. So I'm just gonna copy and paste this into here. And that's that done. What we can do is go on to actually code in our application. First of all we're going to need to include a few headers are going to be hash include f stream, it has to be able to read data from a foil hash include string who's gonna sign the data from the file to a string called CDC, the method depending on what your data is, it could be a number and we're gonna do STD equals belongs to stand in a base if stream and this is basically going to be the form that we're going to be loading. So when the file open, this is actually a good thing to use or a great thing to encompass within a person catch block statement which we've covered previous in this series.
So when you open for progress if you use a try catch for the purpose of this tutorial I won't be in specify the name and I believe use Hello dot txt and do STD string foil output to check if file.is open basically how do you successfully open the file has it located I'm just going to loop through it under the built in function into extreme shades, foil dot mean not foil dot e o f is basically stands for while it's not the end of the file, just go through it and print out I'll put basically put the files contents into the file output string variable that we created right here. And then we'll print it out so cm foil codebook STD and learning what we can do is an else statement. So else STD foil fail to open STD and line and now it's going to close that far last once we're done with reading this folder close life we want you to open up the debug area was this yes I believe to the safety of our T f. Let's do something now.
Not sure what we're going to do Just go back to TextEdit create a new for me sublime. So now simply sublime keyword Hello in the keyword I'm in this text. Hello save that to desktop I don't call this will dot txt get off that copy and paste this here and I will change this to well dot txt runner. Let me get the word hello printed out so it's working a Okay, we just changed the estimates put number five here, this file doesn't exist. As you can see right here. Over on this it says file file to open the gotta did the file successfully open?
No. So he basically printed out file fail to open. That's it for all In your file in the next part of this series, we're going to look ahead to go to a file. If you have any questions feel free to message us at support at some systems coda UK email will be in the description you can comment on this video or just directly messages via YouTube, or the recording for source code will also be in the description underneath. Thanks for watching and I hope you have a great day.