在Objective-C中使用fopen() [英] Using fopen() in Objective-C

查看:227
本文介绍了在Objective-C中使用fopen()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于这段代码中的错误而使我不断崩溃,我对此感到困惑:

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);

错误是:打开Length200Vector.txt时出错:不允许操作.

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

该文件位于我的项目的Resources文件夹中,并且此行在.mm文件中执行.在.cpp文件中的同一项目中,我实际上使用的是完全相同的代码,而没有问题.似乎无法弄清楚这一点...

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...

谢谢

推荐答案

关于您的评论,这是一个iOS应用:关于iOS沙箱,您不允许执行修改( fopen()中的"w"文件可访问iOS应用程序中除文件"Documents"目录(或用于常规应用程序资源的〜/Library/Application Support/"bundle ID"目录中的文件)以及临时文件中的文件以外的任何内容通过调用NSTemporaryDirectory()函数[1]返回的目录.

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] 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆