In C, how do I burn a file from a MacOSX application suite

I want to debug some things in my application by dumping text to a file, for example:

FILE *file; file = fopen("/tmp/file.txt","a+"); fprintf(file,"%s\n", "silly debug message"); fclose(file); 

This works when I run the file as. / myapplication. But when I bundle it as an application package (using gtk-osx-application and gtk-mac-bundler ) and run the application package, the file will not be created. He fails and continues execution.

Any ideas why? Is there a sandbox?

+4
source share
3 answers

OK, this is no longer a question. The problem was that I had an additional MyApp.app in the / root folder in addition to the application that I thought I tested in / Application. When using "open -a MyApp", the old version of the application in the root folder was used.

Here are a couple of hours of my life that I will never return. Well. Thanks for the tip, folks. I know more about NSLog (), CFShow (), CFString and syslog (), and it is very useful nonetheless.

0
source

Yes, your application package is isolated, so you cannot write to arbitrary places in the file system (e.g. /tmp ). To access a temporary directory specific to your application, use NSTemporaryDirectory instead ;

 import <NSPathUtilities.h> NSString* tempPath = NSTemporaryDirectory(); 

See more information about file system access here .

+6
source

When I create a test application with a sandbox and add additional error checking:

 FILE *file = fopen("/tmp/file.txt","a+"); if (file != NULL) { fprintf(file,"%s\n", "silly debug message"); fclose(file); } else { NSLog(@"Failed to create file: %d", errno); } 

I get:

 2013-02-09 10:19:43.680 WriteTest[11988:303] Failed to create file: 1 

From the command line:

 $ perror 1 1 : Operation not permitted 

Thus, you cannot write to /tmp in the application sandbox. You need to follow the Sandbox Application Design Guide to determine where you can write files, possibly using Core Foundation and features like CFCopyHomeDirectoryURL() to get the location of files that you can write to an isolated environment.

+1
source

All Articles