0

When I was creating my project with cpp on Xcode, I found a problem that I could only use a full path to open a file.

If I want to read a file, even though this file is under my project dir, I need to enter the full path.

Reading a config.txt under my project dir. config.txt directory: "/Users/MYNAME/Desktop/MYPROJECT/TextFolder/" Project directory: "/Users/MYNAME/Desktop/MYPROJECT/"

ifstream my file ("/Users/_MYNAME_/Desktop/MYPROJECT/config.txt");

How can I use relative path?

user3390652
  • 132
  • 1
  • 13
  • You can still use a relative path, but you have to change the project settings so that when Xcode runs your program it sets the working directory to someplace else. Or move the file to the default working directory. – Some programmer dude Dec 15 '15 at 13:43
  • I have read something like ${PROJECT_DIR} $PROJECT_DIR or sdk something like these. but non of them works for me. – user3390652 Dec 15 '15 at 13:49

1 Answers1

0

I had read some other threads asking how to use relative path on Xcode, and I have found a few ways to do that, while only the approach suggested by user SSteve in New to Xcode can't open files in c++? works for me.


Using SSteve's way to copy files, I can simply use the following code to open a file.

ifstream my file ("config.txt");, or ifstream my file ("TextFolder/config.txt");


Yet, there are still some other suggestions. Some suggested to change the path in working directory by editing scheme. But I doubt if this approach allows me to download the code on another mac and just run the code.

Community
  • 1
  • 1
user3390652
  • 132
  • 1
  • 13