Mac osx c++ .app 找不到文件 fopen()

发布于 2024-12-05 05:00:00 字数 249 浏览 1 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无人问我粥可暖 2024-12-12 05:00:00

问题是 .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 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.

You can actually browse the directory structure of your .app by right-clicking on it and choosing View 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 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.

楠木可依 2024-12-12 05:00:00

您必须在 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.

蓬勃野心 2024-12-12 05:00:00

如上所述,.app 是一个包,或者是 Mac OS X GUI 将其视为文件的特殊文件夹。 .app 不是正在运行的内容,正在运行的是 your.app/Contents/MacOS/your,当前目录指的是 your.app /内容/MacOS/。如果您不介意在应用程序中使用 Objective-C(如果您尚未链接 Foundation.framework,我建议您找到另一种方法),您可以这样做来获取应用程序包:

const std::string GetApplicationDirectory()
{
    id info = [[NSBundle mainBundle] infoDictionary];
    NSString *bundleID = [info objectForKey: @"CFBundleIdentifier"];

    NSWorkspace *wp = [[NSWorkspace alloc] init];
    NSString *app   = [wp absolutePathForAppBundleWithIdentifier: bundleID];

    const char *str = [[app substringToIndex: [app length] - [[app lastPathComponent] length]]] cStringUsingEncoding: NSASCIIStringEncoding];
    return std::string( str );
}

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 is your.app/Contents/MacOS/your, and the current directory refers to your.app/Contents/MacOS/. If you don't mind using Objective-C in your app (if you're not linking Foundation.framework already I'd advise you find another way), you can do this to get the path of the app bundle:

const std::string GetApplicationDirectory()
{
    id info = [[NSBundle mainBundle] infoDictionary];
    NSString *bundleID = [info objectForKey: @"CFBundleIdentifier"];

    NSWorkspace *wp = [[NSWorkspace alloc] init];
    NSString *app   = [wp absolutePathForAppBundleWithIdentifier: bundleID];

    const char *str = [[app substringToIndex: [app length] - [[app lastPathComponent] length]]] cStringUsingEncoding: NSASCIIStringEncoding];
    return std::string( str );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文