简体   繁体   English

在Objective-C中使用fopen()

[英]Using fopen() in Objective-C

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. 错误是:打开Length200Vector.txt时出错:不允许操作。

The file is residing in my Resources folder for my project and this line is being executed in a .mm file. 该文件驻留在我的项目的Resources文件夹中,该行正在.mm文件中执行。 Within the same project in .cpp files I am using practically the same exact code which runs without a problem. 在.cpp文件中的同一项目中,我使用的几乎完全相同的代码运行没有问题。 Can't seem to figure this one out... 似乎无法想出这一个......

Thanks 谢谢

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]). 关于您的评论,这是一个iOS应用程序:您不允许(就iOS沙箱而言)执行修改 (fopen()中的“w”)文件访问iOS应用程序中除了位于应用程序中的文件之外的任何内容“文档“目录(或一般应用程序资源〜/ Library / Application Support /”bundle ID“目录,以及临时文件,通过调用NSTemporaryDirectory()函数[1]返回的目录)。

Get access to the "Documents" directory by something like this 通过类似的方式访问“Documents”目录

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. 如果您有一个资源文件,您需要在应用程序执行期间进行修改,您必须先将其复制到“Doc​​uments”目录,然后进行修改。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM