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

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

Posted by: admin January 4, 2018 Leave a comment

Questions:

I’ve been using windows in a class I’ve been taking but I am trying to run a basic code to figure out how to open/close/input/output from files on Xcode and the code I usually use on visual studios isn’t working any idea why? thanks!

#include <string>
#include <iostream>
#include <fstream>
using namespace std;

int main()
{
    ifstream fin;
    ofstream fout;
    string input;

    fin.open("inputFile.txt");
    if(fin.fail())
        cout << "File failed to open." << endl;
    fin >> input;

    fout.open("outputFile.txt");

    fout << input;


}
Answers:

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.

Questions:
Answers:

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.

Questions:
Answers:

As suggested by nepete, edit the scheme, but use $PROJECT_DIR as the custom working directory. Helps with moving the project around, or working in two different environments (e.g., home and office).

BTW. $PROJECT_DIR is one of the Xcode Environment Variables, and also helps with passing file names as command line arguments to programs (settable under “Arguments” in the scheme).

Questions:
Answers:

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.

Questions:
Answers:

I’m guessing you have inputFile.txt in the folder that contains your source code. That’s not going to work. You need to put it in the folder that contains the generated executable. To find that folder, right-click on your app under Products and select Show In Finder.

enter image description here

This image shows what it looks like for a command line program. It also shows the Finder window that was opened. As you can see, it is a different folder than the one containing the source code.

Command Line version