Xcode 4.6. Can't open .txt file

Discussion in 'Mac Programming' started by thrill4rishabh, Mar 20, 2013.

  1. thrill4rishabh, Mar 20, 2013
    Last edited: Mar 20, 2013

    thrill4rishabh macrumors newbie

    Jul 17, 2012
    Hi, I am a beginner C++ programmer, beginner Mac user. I cannot open text files for my project, so I tried to run a simple program to read data from a file and it doesn't work.

    I have tried placing the file with the executable, tried placing it with the project file and even tried using the full path and it doesn't work. I'm using Xcode 4.6 by the way.

    Please help.

    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    int main()
        cout << "opening data1.txt....\n";
        ifstream infile("Data1.txt");
        int n=0;
        if (infile.is_open())
            while (infile >> n)
                cout << n;
        return 0;
  2. chown33 macrumors 604

    Aug 9, 2009
    First, you need to understand what a working directory is:

    Second, you need to find out what the working directory is when the program runs. To do that, use Xcode's builtin documentation to look up the C function getcwd(). You can also find references for that function on the internet.

    Third, if you tried a full path, post the exact code for it. Otherwise we're just guessing what you wrote, and you're the only one who knows what was really coded.

    Fourth, Xcode can set the working directory for running a program. See here:

    Or google search terms: xcode working directory.
  3. ArtOfWarfare macrumors G3


    Nov 26, 2007
    I can't remember the exact spot but there's someplace in your project settings where you define which files should be copied during a run. Add to that list that it should copy the files you want to read into the product directory.
  4. thrill4rishabh thread starter macrumors newbie

    Jul 17, 2012
  5. SableRaph macrumors newbie

    May 28, 2013
    Would you mind sharing how you got this working?
  6. chown33 macrumors 604

    Aug 9, 2009
    See post #2:
    The first result from a google search is a StackOverflow article with this:
    Just updating this solution to XCode 4.x

    You go on Product -> Scheme -> Edit Scheme

    On the tab Options you choose your working directory.
  7. SableRaph macrumors newbie

    May 28, 2013
    Actually, that topic solved my problem : http://stackoverflow.com/questions/516200/relative-paths-not-working-in-xcode-c

    In case the link ever dies :


Share This Page