In this video, I'll go over some of the popular IDEs
and where you should place the input files that you're going to read from.
CodeLite I won't do because we've been using it throughout the course,
but we've got windows visual studio code blocks cline and XCode,
and I'll do a really short video on all of them. It's the same little program that all it tries to do is
open a file called test.txt.
And what I found was that in CodeLite, we need to do a ..
/test.text.
In windows visual studio, it's just a file name, and code blocks is also just the file name.
In CLion just like in CodeLite we need to go up a directory
and then provide the name of the file.
And XCode is a little different, so please watch the video if you're using Xcode,
and I'll show you how to set it up so that you can use input files.
Okay. So first let's start with visual studio.
So this example I've got visual studio.
And you can see when I run this sample, it says error opening file because the file doesn't exist.
So what we want to do is create the file. So we can click the resource area and click add
new item and then select text file
and give it the name test.txt, it's lowercase
and press add.
And at this point, that file has been added. We can go back to our c++ file
and execute it at this point.
And notice we're just using test.txt as the file name.
And we should get good to go. There we go. Good to go.
So it's all set. So that's the way you would create an input file in visual studio.
So now let's see how we can get this to work using the code blocks IDE.
For this example, I'm in the code blocks IDE.
And you can see I've got the same file here where I'm trying to open
an input file stream named test.txt.
It's not here right now. So if i try to run, it says problem opening file,
just as we expect. So what we'll do is we'll come up here.
So what we can do is we can come up here to -- we can select our project and say file new
empty file,
do you want to add this new file to the active project?
I'll save the project first. And all we're doing here is we're just creating that file
test.txt.
And make sure that it's in the file test directory or whatever the project directory is.
And we're basically where you see your cpp source file right here.
I'll save. Then it's going to ask me select the targets, that's important,
make sure those are checked, select it.
And at that point, you can see it creates this folder called others with the test.txt in it.
And now if we run this, we're good to go.
Okay. So that's how you would create and a file for input in code blocks.
Next up is CLion. Let's see how we can get the input files to read using CLion.
I'm in the CLion IDE on windows.
And you can see I've got a real simple program
that's going to test opening a file, the file name is test.txt.
If I run this, I can just click that little green arrow right here
or I can click the green arrow up here. I'll just do it right here.
You'll see that what we'll have is we'll have an error that says problem opening file,
just as we expect because the file doesn't exist. So I'm going to come up here to the project,
right click and say new file.
The file name would be test.txt.
I'll save that. And we'll try to run it again.
And I'll click the cpp file again.
Again, we're seeing a problem opening file.
And if we look at the directory structure, you'll see the problem is that
the exe file is being created in here, you can see it right here
file test exe,
so it's a level up. So what you need to do is the same thing we did in CodeLite,
we just basically say ../ we'll save it, we'll run it again.
And we're good to go. So that's how you do this in Clion.
And we'll finish up this section with XCode.
XCode is a little bit different.
So watch the video if you're using XCode, and you need to have input files.
Okay. So I'm on a mac running XCode.
And I've got that simple program here. I'm trying to open a file named text.txt.
If I try to run this,
I'll get an error down here saying problem opening file.
So the file's not there, so let's add the file.
So let's right click here, we'll say new file,
and I'll select mac os, and I'll scroll all the way down to the bottom so it says empty.
And we'll create test.txt.
That's it. So the file's there now. So if we run again,
it still won't find it. We'll have a problem opening file.
The way that XCode works and mac works is that exe file, the app is
stored in some derived data directory really, really far removed from the project.
So the best way to do this on a mac is to come up to product
then select scheme and edit scheme
and then look for this working directory right here.
It's under options on run debug.
You select working directory,
and you select -- you say use a custom working directory.
And what we want to choose is we want to search --
and what you want to do is select the working directory
where those files are, your main cpp and your test.txt and you select it.
We close this up now. And now if we run, we should be good to go,
and we are and it will find the file just fine.
Most of the other IDEs will also allow you to change the working directory,
but it's much easier just to change the file path on those,
but on a mac this is the best way to do it.