Using fopen () in Objective-C

I am puzzled by the failure that I keep getting due to an error in 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); 

Error: Error opening Length200Vector.txt: operation not allowed.

The file is in my β€œResources” folder for my project, and this line is executed in the .mm file. Inside the same project, in .cpp files I use almost the exact exact code that works without problems. It can't seem like this ...

thanks

+7
source share
1 answer

As for your comment, this is an iOS application: you are not allowed (from the point of view of the iOS sandbox) to modify ("w" in the fopen () file) to access anything other than the files located in iOS applications in your Documents directories (or for application shared resources: // Library / Application Support / "bundle ID" directory, and for temporary files, the directory returned by calling NSTemporaryDirectory () [1]).

Get access to the Documents directory using something like this

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

If you already have a resource file that you will need to modify at runtime of your application, you will have to copy it to the Documents directory first and then change it.

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

+12
source

All Articles