New to Xcode can't open files in c++? New to Xcode can't open files in c++? xcode xcode

New to Xcode can't open files in c++?


Put your .txt files in the same directory where your main.cpp file is (or anywhere you like).

In Xcode go to Product > Scheme > Edit Scheme > Run test (on the right) > Options (middle top)

Down under Options check “Use custom working directory” and set it to the directory where you .txt files are located.

To work with the files, you will have to specify just file names, e.g. in_file.open("inputFile.txt"); no path is necessary.


Here's a completely different approach: Have Xcode copy the input file for you.

  • Select your project in Xcode
  • Select Build Phases
  • Click the '+' button to create a new Build Phase

Steps 1-3

  • Select New Copy Files Build Phase

Step 4

  • Select Products Directory
  • Click the '+' button to add your file

Steps 5-6

  • Click Add Other

Step 7

  • Select your input file and click Open

Step 8

  • Check the Copy items… checkbox and click Finish

Step 9

Now every time you build your project, the input file will be copied to the same folder as the executable no matter where it is built. Of course, to see the output file, you'll still need to find the executable in Finder.


The answers don't really explain the problem so I thought I'd do that.

When you pass a relative path like "inputFile.txt" to file APIs, it's treated as relative to the working directory when the program is executed. This is the same as the 'working directory' when you use cmd.exe or Terminal.app or command lines in general. The Unix command pwd ("print working directory") displays the current working directory. On Windows running the command cd with no arguments performs the same function. (On Unix running cd with no arguments will change the working directory to the user's home directory.)

When you run a program from the command line, the command line shell sets the program's working directory. When you run a program from within an IDE, the IDE sets the working directory. Since, unlike on a command line, there's no obvious answer for what the IDE should set as the working directory, Visual Studio and Xcode set the working directory to different locations by default: Visual Studio sets the working directory to $(ProjectDir), the directory containing the Visual Studio project file; Xcode sets the working directory to the build products directory, i.e. the location the executable was written to.

Some possible solutions to your problem are:

  1. Do not use a relative path, and therefore don't depend on the working directory. This isn't much help in making the program more portable, because the absolute paths will also differ between platforms, and so you will still have to 'configure' the program for each platform. In fact using an absolute path is worse, because it means your source code must differ, whereas it would be better to keep that difference confined to each platform's build configuration.

  2. Configure the IDE to use your desired working directory. Visual Studio can be configured by right clicking the project, selecting Configuration Properties > Debugging > Working Directory, and setting the working directory to the desired path (potentially using Visual Studio build variables).

    nepete's answer describes how to configure the working directly set by Xcode.

  3. Configure the IDE's build process to copy your data files to an appropriate location. In Visual Studio you would do this in a C++ project by configuring the project's Properties > Configuration Properties > Build Events.

    SSteve's answer covers how to configure additional build steps in Xcode.