I'm trying to help my son with a Programming C++ for Engineers course. I got him set up with Xcode 13.1 on his iMac but I've never used it myself. That was working great until his first assignment that required writing output to a file. We created a new Command Line Tool project and then used the File->New->File... option to create an Empty file. After doing so, selecting that file and looking at the info on the right side of the window shows that the Full Path is pointing to the directory where his main.cpp file is. Yet, when running his code nothing was being written to the file. I had assumed that just referencing the file name in his code would cause it to use a local reference and look in the same directory as main.cpp. After much Googling I found a reference that said to go to Product->Scheme->Edit Scheme... then select the Options tab in the window that opens and change the Working Directory from "$(BUILT_PRODUCTS_DIR)" to the directory containing the main.cpp file. That got the programming working correctly. My questions are:
Is there a preference setting that would tell it to always use the current project directory so that we don't have to change this every time?
Where does "$(BUILT_PRODUCTS_DIR)" point to?
What should we be doing differently?
I have the following problem. I would like to open and save a string to file.
ofstream file("file");
file << "example" << endl;
file.close()
Rather simple stuff. The problem is... the code above doesn't seem to create any files in my project directory. I have tried to use "locate" in terminal to find my file - withoud any results. What is interesting - I can open the file using ifstream and read from it, without any particular problems.
What the heck is going on here? Where should I look for the file created?
P.S. On Windows everything works 100% fine.
in my project directory
Where did you read that this is where to look? We should collaborate and correct that resource.
I have tried to use locate in terminal
It probably hasn't been indexed yet.
You need to look in the current working directory of the terminal that started the program.
The file exists in the directory, and I've tried running Visual Studios in Administrator Mode. However, ifstream can not find the file I give to it.
Here is the code I am using:
std::ifstream instream;
instream.open("appdata.txt");
if (!instream)
{
std::cout << "Could not find appdata.txt!";
}
But I am always greeted with Could not find appdata.txt! when I run the program.
Here is a picture of my directory, for proof that I have it spelled correctly and it exists.
So, my question is, am I missing something so glaringly obvious that I am glazing over it each time I look? I can not figure out for the life of me why instream can not open appdata.txt.
This is a problem with current directory being set to something else than a dir where your file is (usually your home folder if executing from explorer).
Try executing the program from command line from directory where your file is.
EDIT
If you want to set the working directory to some specific location, check this: https://msdn.microsoft.com/en-us/library/aa363806.aspx
Add the file by right clicking on project name on visual studio interface.
This will keep your file in the right directory.
If you want to add in the directory by yourself, first add a file using the method I said above and the find which is the folder you should keep so that you can use that file by mentioning just the file-name. And then you can add your files in that folder.
http://www.cplusplus.com/doc/tutorial/files/
I just finished executing this but I didn't get any file on my desktop.
So where does the .txt file get placed on my computer?
// basic file operations
#include <iostream>
#include <fstream>
using namespace std;
int main ()
{
ofstream myfile;
myfile.open ("example.txt");
myfile << "Writing this to a file.\n";
myfile.close();
return 0;
}
Damn... I've seen this post never answered many times..
Here's the solution
In your project navigator when you're working in your current project there are many files. Try products (if I remember) and look for the executable file. Now go to the properties inspector or whatever is called. (Right side of your Xcode.)
There you will find somewhere a part called PATH the path that is written over there. It's the path where the executable runs. That means there is where you'll find all the files you create with your program.
Try it... that's how I manage and look my .txt files since sometimes I wanna give them some kind of formatting.
I hope I've solved your problem... cheers! ;)
Click on the executable in the Products tab on the left hand side, and you should see the path to it displayed on the right in the utilities section. This is where they are saved. On my computer this path is:
/Users/myName/Library/Developer/Xcode/DerivedData/CurrentXcodeProject-adsnvetbleazktcgitfymfcbhkkt/Build/Products/Debug/filename.txt
In the "current directory", meaning the working directory of the environment from which you ran the executable.
I can't tell you what that is, but in basic cases it may be the directory where the executable is located. In many other cases it may not be.
Performing a search for the file on your hard drive will reveal its location to you.
Right click on your product and select "Show in Finder". Thats opens the path to where the output files go. Also you can add input files there to be read in by your program.
Found best answer here:
File creation in C++ on Xcode
Basically you can specify a known directory so that all files created go there.
If you are on Mavericks Right click in your user folder with your documents and pictures and stuff (~/Users/yourusername)
Now right click and view options. Check "show library". Now follow to the path that the first user above said.
What I've always done when an application saves a file and I don't know where... is just go back to that application, and hit File>Save As...
So re-open Xcode and go to the "File" menu, and click "Save As..."
It will show you the same directory it just saved to.
I have checked the "Current Directory" but I believe I have to specify the location. Not even
~User/Desktop
will work.
What I do is create an empty file with a the correct name on my desktop then drag that file into my Xcode project to get its path. I then delete the file.
Same with adding files to my project. I can add them from the File menu, but I will need to drag them in to the spot in my code where I want to reference them.
It is kind of convoluted but it is the only system I have.
Some of the answers are confusing. Here's the simplest solution.
1. Expand your Products folder on the left-hand side
2. Right click on the terminal icon(sorry I do not know what it's called)
3. Click show in Finder.
You should be able to see your output file in there.
" Where does Xcode create .txt files to? "
You can decide where you want: follow these->
" Product->Scheme->EditScheme "
chose options tab in the new popup "options"
"working directory" tick "use coatroom working directory" and locate the folder where you want.
The .txt file that you "create", or that you want too "read" has to keep in this folder.
In 2022, click on the project name in the "breadcrumbs" area above the editor. Select the correct thing in "Products", then in the right area, see "Full Path".
I am working on figuring out how to use Xcode 4 to debug c++ projects.
I have basically copy pasted a working c++ executable that when compiled from the terminal ran fine.
However, i was thinking it might be nice to use Xcode for debugging. So I am trying to migrate the single .cpp file into Xcode as a command line tool.
I need to read in a file called numbers.txt (which I supply through a command line argument) which is located in my project directory, and then out put to a file (whose name I also specify as an argument.)
The problem I am running into is that the files that are supplied as command line arguments are failing to open.
ifstream in;
ofstream out;
in.open(argv[1]);
out.open(argv[2]);
I have checked to make sure that the arguments are being properly passed and are named correctly. The ifstream in is being supplied with `numbers.txt', which I want to open a text file that I already have.
However when I check to make sure the ifstream is open:
if(in.is_open() == false){
cerr << "Unable to open input file" << endl;
return 1;
}
I get the error.
I suspect this has something to do with how Xcode organizes the project.
my numbers.txt file is just sitting in the Xcode project folder, and I have only one .cpp class and one product, the executable.
anyone know what I am missing here?
The executable built by Xcode is in a different folder than the project. Passing in the name of the file without an absolute path before it will cause the executable to look for it in the wrong place, which is why it can't be found. Some of the possible solutions are to include the file as part of the build process (so it ends up in the same directory as the executable) or to pass the file to be opened by its absolute path. There are other ways to solve the problem, too, but hopefully that should be enough to get you started.
Old thread, but i have faced the same problem now, and it is easy to solve. Just copy the file in the build phase.
Here is an screenshot of the final result (note the destination, subpath and checkbox):