Mac osx c ++ .app找不到文件fopen() [英] Mac osx c++ .app cannot find a file fopen()

查看:807
本文介绍了Mac osx c ++ .app找不到文件fopen()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件夹在同一目录下,我的mac .app,在它是一个文件test.ssm。正在尝试通过此函数打开:

I have a folder in the same directory as my mac .app and in it is a file called test.ssm. This is trying to be opened by this function:

FILE *input_file = fopen("./data/maps/test.ssm", "r");

问题是应用程序不断得到代码:EXC_BAD_ACCESS,因为它无法打开此文件。我的项目已经在xcode中使用正常coccoa项目设置。这是一个opengl / glut应用程序。

The problem is that the application keeps getting the code: EXC_BAD_ACCESS, because it cannot open this file. My project has been set up in xcode using a normal coccoa project. This is an opengl/glut application.

推荐答案

问题是 .app 是一个包,因此它实际上是一个目录结构,而不是一个正常文件。您的实际可执行文件位于目录 YourApp.app/Contents/MacOSX / 中。因此,当您使用以 ./ 开头的相对路径在C代码中打开文件时,可执行文件实际上是在 YourApp中查找您的文件.app / Contents / MacOSX / 目录,而不是安装 .app 包的父目录。

The problem is that a .app is a package, and therefore it is actually a directory structure as well, not a "normal" file. Your actual executable is inside the directory YourApp.app/Contents/MacOSX/. Thus when you use a relative path starting with ./ to open a file in C-code, the executable is actually looking for your file inside the YourApp.app/Contents/MacOSX/ directory, and not the parent directory that the .app package is installed in.

您可以通过右键单击它并选择来查看 .app 的目录结构。 / code>。

You can actually browse the directory structure of your .app by right-clicking on it and choosing View Package Contents.

如果要将文件放置在您希望从可执行文件访问的文件系统中,请将它们打包到 .app ,或将它们放在 .app 之外,并放置足够的 ../ 在您的文件访问中,让您退出 .app 目录结构,并进入父目录 .app

If you are going to place files in the file-system that you would like accessible from your executable, either package them inside the .app, or place them outside the .app, and place enough ../ in your file access to get you out of the .app directory structure and into the parent directory where the .app is installed.

如果您希望 / data 目录位于 .app 包,那么您只需要在路径中添加足够的 ../ ,即可将您从 / Mac OSX 目录中,并进入 .app 的根目录,其中 / data

If you want your /data directory to be inside the .app package, then you would only have to add enough ../ to your path to get you to out of your /MacOSX directory and into the root of the .app where the /data directory would be found.

最后,如果你需要知道可执行文件所在的绝对路径,你总是可以使用C函数 _NSGetExecutablePath mach-o / dyld.h 中找到(即,你不需要objective-C)。从那里,您可以修改路径以获取文件系统中相对于可执行文件所在位置的任何其他目录,将其修剪到正确的父目录,然后将路径名追加到要打开的文件。

Finally, if you need to know the absolute path where your executable is located, you can always use the C-function _NSGetExecutablePath found inside of mach-o/dyld.h (i.e., you don't need objective-C). From there you can modify the path to get at any other directory in the file-system relative to where your executable is by trimming it to the proper parent directory and then appending the path name to the file you want to open.

这篇关于Mac osx c ++ .app找不到文件fopen()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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