Can't access files on data folder using relative path

Hi guys,

I am new here so sorry if this is a very simple issue.

I don’t know what I am doing wrong but I can’t access files on the data folder if I use a relative path.

What I am trying to do is as simple as load an image.

with an absolute path works:
myImage.loadImage("/Users/rogerp/Documents/of_preRelease_v0061_osxSL_FAT/openFrameworks/apps/b-reel/drawingIn2D/bin/data/me.jpg");

with a relative path doesn’t work:
myImage.loadImage(“me.jpg”);

I don’t get any warining or error, but the image just doesn’t apear.

I am using oF v0061 and xCode 3.2.1 in Snow Leopard 10.6 OSX.

To create the project I have followed the instructions on this video: http://www.vimeo.com/755955

Anyone knows what happen?

Thanks in advance!

/roger

can you check the application preferences (when you run the app, under the menu or command-comma). Can you see if “use current working directory” is set, it shouldn’t be:

http://forum.openframeworks.cc/t/can’t-find-data-files-in-006/2326/0

take care,
zach

Thanks for the quick response :wink:

I have seen this post but I have it unchecked.

The only thing checked there is “Debug mode”.

Any other siggestion? :S

Strange, I’m having the same problem. No files can be loaded from the data folder in my current xcode project. All my other projects and examples work fine. Putting an absolute path works, but the file isn’t found if it’s relative.

Did you manage to fix this? I may just try creating a new project and seeing if that fixes it. Perhaps I changed some build options by accident?

Hey, I’m still trying to fix it but with no success.

Please, let me know if you find the solution.

thanks! :wink:

I’m stumped. I trashed the xcode project file and replaced it with the emptyExample one, added all the source files/addons to the project to start from scratch and it’s still not working. Absolute paths work fine though!

Ditto - let me know if you come across the solution as well!

Ok, I’m now un-stumped. It works.

Trashing the project and using the emptyExample one as a base fixed it. Try that and double check the “Use Working Directory” setting and hopefully that will fix it for you.

Let me know!

James

I’ve also been suffering these issues for some time. (The osx app settings don’t seem to be my issue). Pretty much every project I make requires at least one re-build at some point during development, just to get the /data/ directory back. I have not been able to nail the cause, other than to say it is a cross platform issue (linux & osx).

I’ve rebuilt two projects today. Once I’ve finished the install I am working on, I am going to make some time to try and find the cause.

Edit: Just seen a similar approach was used here: http://forum.openframeworks.cc/t/can’t-find-data-files-in-006/2326/0
I’ve applied the code across a number of projects and it has fixed the issues.

Right… I think I have an initial fix/workaround for this on OSX. From what I can see, the ‘initial working directory’ can get screwed up quite easily in an XCode project. I don’t know what causes it, but I do know that as I construct projects it somehow changes and I end up having to rebuild the project.

To stop the ‘working directory’ from being set (or messed up) by XCode, I’ve put together some code that handles it at runtime. It does the following:

  1. isolates at run time the executables directory (within the CFBundle)
  2. sets the current working directory to that

Place this code at the beginning of your setup() before you attempt to load any files with relative paths.

  
  
#ifdef TARGET_OSX      
// Get the absolute location of the executable file in the bundle.  
CFBundleRef appBundle     = CFBundleGetMainBundle();  
CFURLRef	executableURL = CFBundleCopyExecutableURL(appBundle);  
char execFile[4096];  
if (CFURLGetFileSystemRepresentation(executableURL, TRUE, (UInt8 *)execFile, 4096))  
{  
	// Strip out the filename to just get the path  
	string strExecFile = execFile;  
	int found = strExecFile.find_last_of("/");  
	string strPath = strExecFile.substr(0, found);  
	  
	// Change the working directory to that of the executable  
	if(-1 == chdir(strPath.c_str())) {  
		ofLog(OF_LOG_ERROR, "Unable to change working directory to executable's directory.");  
	}  
}  
else {  
	ofLog(OF_LOG_ERROR, "Unable to identify executable's directory.");  
}  
CFRelease(executableURL);  
#endif  
  

I am no XCode guru, so interested to hear feedback on this approach. Hopefully it helps someone out.

1 Like

After trying several solutions and no dice the above code got my image loading! Thank you so much!

Thanks a lot lukemalcolm, it worked for me! :smiley:

Glad this is working out for people. Might submit it to be considered as part of OF. I use it so often and a few others seem to too.