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