Mac osx c++ .app 找不到文件 fopen()
我的 mac .app 在同一目录中有一个文件夹,其中有一个名为 test.ssm 的文件。这是尝试通过此函数打开:
FILE *input_file = fopen("./data/maps/test.ssm", "r");
问题是应用程序不断获取代码:EXC_BAD_ACCESS,因为它无法打开此文件。我的项目已使用普通的 coccoa 项目在 xcode 中设置。这是一个 opengl/glut 应用程序。
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");
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是
.app
是一个包,因此它实际上也是一个目录结构,而不是一个“普通”文件。您的实际可执行文件位于目录YourApp.app/Contents/MacOSX/
中。因此,当您使用以./
开头的相对路径在 C 代码中打开文件时,可执行文件实际上是在YourApp.app/Contents/MacOSX/
中查找您的文件。 code> 目录,而不是安装.app
包的父目录。实际上,您可以通过右键单击
.app
来浏览它的目录结构,然后选择查看包内容
。如果您要将文件放入文件系统中并希望通过可执行文件进行访问,请将它们打包在
.app
内,或将它们放置在.app
外部,并在文件访问中放置足够的../
以使您脱离.app
目录结构并进入.app
所在的父目录> 已安装。如果您希望
/data
目录位于.app
包内,那么您只需在路径中添加足够的../
让您离开/MacOSX
目录并进入.app
的根目录,其中可以找到/data
目录。最后,如果您需要知道可执行文件所在的绝对路径,您始终可以使用在
mach-o/dyld.h
中找到的 C 函数_NSGetExecutablePath
(即,您不需要 Objective-C)。从那里,您可以修改路径以获取文件系统中相对于可执行文件所在位置的任何其他目录,方法是将其修剪到正确的父目录,然后将路径名附加到要打开的文件。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 directoryYourApp.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 theYourApp.app/Contents/MacOSX/
directory, and not the parent directory that the.app
package is installed in.You can actually browse the directory structure of your
.app
by right-clicking on it and choosingView Package Contents
.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.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.Finally, if you need to know the absolute path where your executable is located, you can always use the C-function
_NSGetExecutablePath
found inside ofmach-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 OS X 中提供绝对文件路径,因为 Mac OS X 是为使用文件对话框或类似的东西而构建的。
You have to provide absolute file path in Mac OS X, because Mac OS X is built to work with file dialogs or something like that.
如上所述,
.app
是一个包,或者是 Mac OS X GUI 将其视为文件的特殊文件夹。.app
不是正在运行的内容,正在运行的是your.app/Contents/MacOS/your
,当前目录指的是your.app /内容/MacOS/
。如果您不介意在应用程序中使用 Objective-C(如果您尚未链接 Foundation.framework,我建议您找到另一种方法),您可以这样做来获取应用程序包:As said above, the
.app
is a package, or a special folder that the Mac OS X GUI treats like a file. The.app
isn't what's being run, what's being run isyour.app/Contents/MacOS/your
, and the current directory refers toyour.app/Contents/MacOS/
. If you don't mind using Objective-C in your app (if you're not linkingFoundation.framework
already I'd advise you find another way), you can do this to get the path of the app bundle: