I am running a Qt program on embedded linux. My program needs to be able to read some information from a text file that is located in the same folder. When I run the program everything works OK.
I now want to make my program start at startup, so I add the line
/home/my_program_name -qws &
to one of the files in the /etc/init.d folder. When I restart my machine, the program runs but it doesn't seem to be able to read the text file. It looks like that folder has not been initialized or read when the program starts.
I should mention that the /etc/init.d folder contains several files that are executed in alphabetic order and that the Qt program is the last one to be executed (i.e. at the end of the last file).
How do I make my program access the text file at startup?
In order to open a file with relative path (e.g. same directory as application), You need to make sure current directory is program's location.
You set current directory to application's directory by using following.
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(myapplication);
QApplication a(argc, argv);
QDir::setCurrent(qApp->applicationDirPath());
...
or
int main(int argc, char *argv[])
{
Q_INIT_RESOURCE(myapplication);
QApplication a(argc, argv);
QDir::setCurrent(QFileInfo(argv[0]).absoluteDir().absolutePath());