2

I'm new to Xcode. How can I add a file to the build output directory? I have a file.txt which I want to open in C++.

The following code...

string line;
ifstream file ("file.txt");
if (file.is_open())
{
   while (file.good())
   {
       getline (file,line);
       cout << line << endl;
   }
   file.close();
}

... results in Unable to open file.

I found out that the executable is deployed to /Users/myusername/Library/Developer/Xcode/DerivedData/IAIK_CMS_Test-dfmszjneldfwjffsmjpmtiepyzej/Build/Products/Debug/

I've tried to add the files in the project settings to the "Copy Files" in the "Build Phases". For details see this screenshot: https://i.sstatic.net/QhkIT.png

Edit: I'm looking for something like Eclipse's "add to Build Path" for Java.

Does anyone have a suggestion?

7
  • Not answer but maybe it would be easier to not hard code the path and take it is a program parameter. Also argv[0] will be how the program is run, so you can load the file from the directory path. (so up to the last forward slash. Commented Feb 15, 2012 at 14:24
  • 1
    I'm no XCode expert, but it looks like you're copying it to /usr/share/man/man1, which is probably not what you want. Commented Feb 15, 2012 at 14:27
  • @111111, I thought about it but the problem is that it depends on the build mode you choose. For example when I build in debug mode the executable is deployed to .../Debug/ in release mode to .../Release/ and so on. Commented Feb 15, 2012 at 14:27
  • @molbdnilo, it doesn't change anything when I change the path there. I guess the "Copy Files" section is the wrong one for this issue. I added it to the question just for details. Commented Feb 15, 2012 at 14:30
  • Chris, so why not either read from ../file.txt (the common parent of Debug and release or read from /home/you/file.txt (ie explicit/absolute path to your home directory). Commented Feb 15, 2012 at 14:44

1 Answer 1

7

Xcode5 is out, still no answer :)

For Xcode5 in C++ project settings, choose your app target, select "Build phases" tab, unfold "Copy Files" section, set "Products Directory" for Destination, leave Subpath field blank, uncheck "Copy only when installing" and add necessary file (file.txt) to the list using plus sign, like that:Copy file on build

And use "file.txt" path to access your file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.