Trouble with paths in Xcode

Discussion in 'Mac Programming' started by Fenrir, Mar 15, 2007.

  1. macrumors newbie

    Joined:
    Mar 15, 2007
    #1
    A program I've written in Xcode makes use of several files. When I run the program from inside Xcode, everything works fine. However, when I run the executable that's created from outside of Xcode, it doesn't find any of the input files.

    Any ideas on how to fix this?
     
  2. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #2
    A few questions.
    How are you referening the files? Full Paths? What language are you using?
    Can you upload the source?
     
  3. thread starter macrumors newbie

    Joined:
    Mar 15, 2007
    #3
    I'm using a combination of Objective C and C++, and I'm using relative paths to reference the files. The location of the executable is such that the reletive paths are correct.

    It seems as though the program's using a different working directory when it's being run from Xcode than it does when being run as an executable. I just can't seem to figure out why that is.


    Here's an example of the code, though I'm not sure this will help much.
    Code:
    string exampleParagraph = "./ComprehensionExamples/ExampleParagraph.txt";
    ifstream paragraphFile(exampleParagraph.c_str(), ios::in);
    
     
  4. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #4
    Hi
    There was another thread recently similar to this. I think it was the 'Xcode and Input Files' thread.

    b e n
     
  5. macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Location:
    Cork, Ireland.
    #5
    I'd recommend using the full path.

    Find the path of the executable's folder, then add on the relative path to your input files.
     
  6. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #6
    In Xcode, the executable path is:

    Project path->build->Debug(or Release)->application name

    ...which is also the current working directory.

    But when you run the executable outside the working directory, the executable path is the same as the working directory. Many beginners miss the fact that when they click the "build and run" button the working directory IS NOT the project directory, but a location inside that (which I described above).

    Does that help? You don't give any information as to where you have put these input files, and from where you run the final program.
     
  7. macrumors G4

    Joined:
    Jan 5, 2006
    Location:
    Redondo Beach, California
    #7
    You answered you own question: Xcode does a "cd".

    It's really not a good design to have your program use a path relative to the current working directory. Maybe you can change it so it uses a path relative to the user's home directly. Just append the content of the $HOME environment variable to the relative path.
     
  8. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #8
    Can't you use NSBundle to help you access stuff within your own app bundle?
     
  9. macrumors 6502

    Joined:
    Dec 6, 2006
    #9
    That would restrict the program to only being able to read data from within the resources folder of the app. While this is good for images, sound that a program uses, it isn't good if you need data that changes or need to read data from sources. If you need that then I believe it is called NSDefaults allows you to store information about where data should be read from and with the pref panel code you can write new defaults out that will allow the program to read from any directory the user chooses.
     
  10. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
    #10
    How does storing stuff in a prefs file help the OP with his problem. He can't access the file. Knowing how NSUserDefaults works isn't gonna help much when he can't access the file in the first place.

    [NSBundle mainBundle] will return the path of the app. He can then build a path to anywhere in the app bundle from that - if that's where the files are (which seems to the case from his 2nd post). If it's not then use the tilde for the home directory and use NSString's stringByExpandingTildeInPath method.
    Code:
    NSString* fullPath=[@"~/Documents/MyStuff" stringByExpandingTildeInPath];
    
     
  11. macrumors 6502

    Joined:
    Dec 6, 2006
    #11
    The only reason I mentioned the defaults is it allows the program to assume a good general location for where files will be loaded from.

    Maybe the program will only ever been used by the OP, but if this is not the case, can you imagine most Mac users being told to put files in only a certain directory otherwise the app won't work?
     

Share This Page