将fopen与xcode一起使用时为NULL [英] NULL when using fopen with xcode

查看:76
本文介绍了将fopen与xcode一起使用时为NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为iPhone 4inch应用程序使用 C 从文件初始化多维数组,但无法使用 fopen 打开文件.每当我尝试此操作时,我都会得到 NULL :

I am trying to initialize a multidimensional array from a file using C for a iPhone 4inch app but I can't open up the file using fopen. Whenever I try this I get a NULL:

FILE  *f;
f=fopen("/level1.rez", "r");

if (f == NULL)
{
    printf("Error Reading File\n");
    //exit (0);
}

我不确定如何使用 C 打开文件.

I am not sure how to open files using C.

我已经尝试过了:

我使用 getcwd 打印了当前的工作目录,但是我得到的只是"/",当我将其附加到文件名时,我仍然得到 NULL .

I printed out the current working directory using getcwd but all I got was "/" and when I attached that to the file name I still got NULL.

我读到,如果您转到 product>方案编辑方案,然后选择可以更改当前工作目录的选项,但我看不到该选项.

I read that if you go to product > scheme > edit scheme then options you can change the current working directory but I don't see that option.

我还读到您可以使用绝对路径,例如:/users/name/desktop/program 但是我是iOS开发的新手,所以我不知道这是个好主意.

Also I read that you can use absolute paths like: /users/name/desktop/program but I am new to iOS development so I don't know if that is a good idea.

那么如何使 fopen 正常工作?

推荐答案

您可以在iOS中指定绝对路径,但示例中的路径可能在Mac OS中使用,但布局有所不同.您可以按照您的说明指定fopen()的路径,但是要弄清楚该路径的第一部分到底是什么,还有很多工作.

You CAN specify absolute paths in iOS, but the path in your example is probably used in Mac OS, which is laid out a little differently. You can specify paths to fopen() as you say, but there is more work to finding out what the first part of that path really is.

iOS将所有AppStore应用程序放入具有随机生成的沙箱目录名称的文件夹中.它基本上是GUID的十六进制字符串.因此,您需要使用iOS框架中的方法来获取文件路径(或URL)的第一部分.

The iOS puts all AppStore apps into folders with randomly generated sandbox directory names. It is basically the the hexadecimal string of a GUID. So you need to use methods from iOS frameworks to get the first part of the path (or URL) to your file.

如果该文件是应用程序捆绑包的一部分,因此它可以随应用程序一起提供,那么您将需要使用NSBundle方法来查找文件的路径.

If the file is part of the app bundle so it can ship with the app, then you will need to use NSBundle methods to find the path to the file.

如果在设备上启动应用程序后生成或下载文件,则需要使用NSFileManager方法来确定文件目录的路径.(通常是Documents目录.您可以在沙箱中构建您选择的目录结构.)

If the file is generated or downloaded after the app starts up on the device, then you need to use NSFileManager methods to determine the path to the directory of the file. (Typically the Documents directory. You can build a directory structure of your choice within the sandbox.)

这篇关于将fopen与xcode一起使用时为NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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