10

I am puzzled by a crash I keep getting due to an error at this section of code:

                FILE *fid200;
                fid200 = fopen ( "Length200Vector.txt" , "w" );
                if (fid200 == NULL)
                    perror("Error opening Length200Vector.txt");
                for (int n = 0; n<200; n++) {
                    if (n == 0) {
                        fprintf (fid200, "%f", self.avgFeatureVect[0][n]);
                    }
                    else {
                    fprintf (fid200, ", %f", self.avgFeatureVect[0][n]);
                    }
                }
                fprintf (fid200, "\n");
                fclose(fid200);

The error is: Error opening Length200Vector.txt: Operation not permitted.

The file is residing in my Resources folder for my project and this line is being executed in a .mm file. Within the same project in .cpp files I am using practically the same exact code which runs without a problem. Can't seem to figure this one out...

Thanks

walkytalky
  • 9,453
  • 2
  • 36
  • 44
Kevin_TA
  • 4,575
  • 13
  • 48
  • 77
  • 1
    Does the code run `chdir(2)` or `fchdir(2)` at some earlier point in execution? What's the output of `ls -l Length200Vector.txt ; ls -ld . ; id` ? Unrelated -- why the complicated `if (n == 0)` that apparently does nothing? – sarnold Nov 17 '11 at 23:52
  • 4
    Put this before your `fopen` line: `NSLog(@"wd = %@", [[NSFileManager defaultManager] currentDirectoryPath]);` Does it print your Resources directory path? – rob mayoff Nov 17 '11 at 23:59
  • 2
    Is this a Mac OS X app or an iOS app? – rob mayoff Nov 18 '11 at 00:00
  • Remember that `perror()` just prints an error message; it doesn't terminate your program. IF the `fopen()` call fails, you try to write to the file anyway (and all the `fprintf()` calls should fail as well). – Keith Thompson Nov 18 '11 at 00:34
  • sarnold - chdir(2) and fchdir(2) do not run. The n==0 is so the first entry is not preceded by a ",". rob mayoff - this is an iOS app and the NSLog statement prints: wd = /private/var/mobile/Applications/DF60ABC3-95A7-40DD-A3F2-D1FD493138E4/cashconverter.app – Kevin_TA Nov 18 '11 at 15:41

1 Answers1

13

Regarding your comment that this is an iOS App: you are not allowed (in terms of the iOS sandbox) to perform modifying ("w" in fopen()) file access to anything in iOS applications other than to files located in your applications "Documents" directory (or for general application resources the ~/Library/Application Support/"bundle ID" directory and for temporary files the directory that is returned by a call to the NSTemporaryDirectory() function[1]).

Get access to the "Documents" directory by something like this

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docs_dir = [paths objectAtIndex:0];

If you have a resource file already that you will need to modify during the execution of your app, you will have to copy it to the "Documents" directory first, then modify it.

[1] http://developer.apple.com/library/ios/#documentation/FileManagement/Conceptual/FileSystemProgrammingGUide/AccessingFilesandDirectories/AccessingFilesandDirectories.html#//apple_ref/doc/uid/TP40010672-CH3-SW1

cli_hlt
  • 7,072
  • 2
  • 26
  • 22
  • 2
    That's not quite right. There are directories other than the documents directory you can write to (for example, the caches/temp directories, other places in your library directory). The key is that you can't write to your app bundle, which is where your resources live. – Jesse Rusak Nov 18 '11 at 23:15
  • You are right. I will look up the reference and edit my answer accordingly. – cli_hlt Nov 18 '11 at 23:20
  • I fixed this by changing the settings in Targets -> Signing and Capabilities -> App Sandbox -> File Access Type -> User Selected File permission and access. – Pierre Dufresne Apr 28 '20 at 03:23