fopen with a .app file [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-12-14 03:13:05

问题


I am working on a Qt project which maps vowels onto a chart that have the *.sym format.

My goal is to load an initial IPA chart like this.

I have the *.sym files and I can load them after my application starts, but I'm not really sure where my executable is executing.

I have a directory format (after building) like this

Project
|_ Source
|_ Build
  |_ Source
    |_ Charts
      |_ load_at_start.sym
    |_ Project.app
      |_ Contents
        |_ MacOS
          |_ Project (executable)

This makes using an fopen call quite difficult. I assumed that fopen would consider the current working directory as the place where the executable rested, so I tried something like this...

    FILE *stream = fopen("./../../../charts/load_at_start.sym", "r");

but it doesn't work. Can anyone help me?


回答1:


You'll want to make sure the .sym file ends up within the app bundle (the .app directory), specifically under Contents/Resources.

Then you can use QCoreApplication::applicationDirPath to obtain the directory of your application (e.g. "/Applications/Project.app/Contents/MacOS/") and append the relative path to your file (e.g. "../Resources/charts/load_at_start.sym").

Something like this:

QDir dir(QCoreApplication::applicationDirPath());
dir.cd("../Resources/charts");
FILE* stream = fopen(dir.filePath("load_at_start.sym").toUtf8().constData());

Although you might want to consider using Qt's I/O facilities instead of C's.



来源:https://stackoverflow.com/questions/26902827/fopen-with-a-app-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!