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

    macrumors newbie

    Joined:
    Jul 17, 2012
    #1
    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.

    Code:
    
    #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;
                
                cout<<"\b";
                
            }
            infile.close();
        }
        else
        {
            cout<<"ERROR";
        }
        return 0;
    }
    
    
     
  2. macrumors 603

    Joined:
    Aug 9, 2009
    #2
    First, you need to understand what a working directory is:
    http://en.wikipedia.org/wiki/Working_directory

    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:
    http://forums.macrumors.com/showthread.php?t=1285568

    Or google search terms: xcode working directory.
     
  3. macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    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. thread starter macrumors newbie

    Joined:
    Jul 17, 2012
  5. macrumors newbie

    Joined:
    May 28, 2013
    #5
    Would you mind sharing how you got this working?
     
  6. macrumors 603

    Joined:
    Aug 9, 2009
    #6
    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. macrumors newbie

    Joined:
    May 28, 2013
    #7
    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