5

I want to use c++ to open a file on Mac OS.

If I run the program under Xcode, the working directory is the same with the program, which is fine. However, if I try to run the program in terminal, the working directory is alway "Users/username". Do you know how to change the working directory to the location of the program?

Here is the sample code:

#include <iostream>
#include <fstream>
using namespace std;
int main (int argc, const char * argv[])
{
    char * dir = getcwd(NULL, 0); 
    cout << "Current dir: " << dir << endl;

    ifstream fin("hi.txt");
    if (fin.is_open()) cout << "File is Open" << endl;
    else cout << "File is not open" << endl;    
    fin.close();
    return 0;
}
Yuchen
  • 30,852
  • 26
  • 164
  • 234
  • Why do you want the program to use its own install directory as the current directory? It probably won't be able to write there (if the software is installed properly). It might be able to read the files a little more conveniently, but that's of debatable merit. OTOH, if I launch the program from `$HOME/src/subdir/myprogram`, I expect to specify file names relative to the directory I started it from, not relative to `/Applications/Doohickey.app/Contents/MacOS` or `$HOME` or something similar. Of course it depends on what the program does...but be cautious about using `chdir()`. – Jonathan Leffler Nov 05 '12 at 05:20
  • I just want to put all the files together. I used to use windows a lot. And when I run a command line program under windows, the file generated by that program is supposed to be in the same location of the program. I get your point. I'll think about it. Thanks very much. – Yuchen Nov 10 '12 at 16:01
  • Remember that Windows is not primarily a multi-user system, and there isn't an analogue to the Unix home directory for each user. This leads programs to scatter data all over the place. On Unix-like systems, storing data somewhere under $HOME is recommended. For example, configuration info might be in a file (or directory) `$HOME/.yourapp`. Look at the Apple 'OS X Human Interface Guidelines' for where to locate stuff on Mac OS X. – Jonathan Leffler Nov 10 '12 at 16:41
  • I'm having a similar issue here and can't fix it based on this solution. I'm desperate for a solution and will compensate with some reputation. http://stackoverflow.com/questions/40920783/xcode-app-no-longer-reads-input-from-the-folder-the-app-is-stored-in – JosephTLyons Dec 02 '16 at 09:31
  • Also offering a bounty to whoever can help me fix it. – JosephTLyons Dec 03 '16 at 10:07

4 Answers4

11

Use the value $(PROJECT_DIR) in the working directory in your scheme debug settings:

enter image description here

Matt Connolly
  • 9,757
  • 2
  • 65
  • 61
  • For **Xcode 5**: Product -> Scheme -> Edit Scheme -> Run "appname" -> Options, and there you find the "Working Directory" option. – Daniel Nov 23 '14 at 23:41
  • I'm having a similar issue here and can't fix it based on this solution. I'm desperate for a solution and will compensate with some reputation. http://stackoverflow.com/questions/40920783/xcode-app-no-longer-reads-input-from-the-folder-the-app-is-stored-in – JosephTLyons Dec 02 '16 at 09:32
  • I'm also going to be offering a bounty to whoever can help me fix it. – JosephTLyons Dec 03 '16 at 10:06
3

You can set a custom working directory for your project in Xcode. In Xcode 4 choose Edit Scheme from the Scheme menu in the project window toolbar to open the scheme editor. Select the Run step from the left side of the project editor. Click the Options button at the top of the scheme editor. Select the Use custom working directory checkbox. Click the button on the right side of the text field to choose the working directory.

enter image description here

Swift Dev Journal
  • 19,282
  • 4
  • 56
  • 66
2

This is a really old post - updating some info for Xcode 12 (Sept 2020)

Step 1). Xcode -> Product -> Scheme -> Edit Scheme (or create a new one)

Screenshot where to find EditScheme

Step 2) RUN(DEBUG), Working Directory(Checkmark) and enter $(PROJECT_DIR) as a starting point.

What item to change/edit in the scheme

user3696153
  • 568
  • 5
  • 15
1

You could use chdir(), see here: Change the current working directory in C++.

Or if you could always just issue a system call (stdlib.h): http://www.cplusplus.com/reference/clibrary/cstdlib/system/. This won't be portable, but it might be good enough for what you need.

Community
  • 1
  • 1
llakais
  • 1,577
  • 1
  • 12
  • 18
  • But do you know how to get the path of the running app? – Yuchen Nov 05 '12 at 04:28
  • `argv[0]` always contains the path (and the program name, of course) – llakais Nov 05 '12 at 13:32
  • Relying on argv[0] might not be a good idea, there is no standard saying that there must always be a full path, there can be just the first part of the command that started it. Solution is to use `GetModuleFileName(...)` or `readlink( "/proc/self/exe", ...)` – Youda008 Jul 05 '16 at 13:46