View Full Version : File path issue...

1st October 2008, 10:39

I am really confused .. :confused:

The problem is of relative fiile path... Please see attachmed image
I want to read info.txt file in res folder. i have specifies the relative path as

QFile file("res/info.txt")

it works fine if the exe is created at test.cpp level but if i create the exe at some other path
the relative path to info.txt is disturbed. Now the file cannot be read....
I need to make path relative to test.cpp not to the applcation exe.

Please help.....


1st October 2008, 11:01
maybe this will help

QFile file("../res/info.txt")


1st October 2008, 11:14
but it tries to search the file relative to application executable so...every time if the executable is placed at a different path. the relative path will change.

1st October 2008, 11:30
If you run the program from an IDE, working directory will be the one of the project.
You can do one thing,,, set the working directory of the application. In that way you can always be sure of the relative paths

Hope i am making sense :D

1st October 2008, 11:32
I probably don't understand what you mean.
so, do you have the next structure of your application directory

| |---info.txt<file>


1st October 2008, 11:43
| |---info.txt<file>
|build<dir> -- application.exe<file>


| |---info.txt<file>
|build<dir>test<dir> -- application.exe<file>

i mean to say that different users of my code will have different paths of storing the executable as per their personal preference .. so how can i be sure that my path to access info.txt does not change with it...
because the path is relative to application executable not to the file in wihch it is used.

1st October 2008, 11:49
hm, very intresting.
why an aplication can't be located in one place?
and for different users you can create needed directory. so, if the application will always be in one place then problem for determination a needed directory never appear.

1st October 2008, 12:19
But how can govern the others users visual studio and Xcode settings...
Can Qt Resource System help in this case..?
If i compile the resourse in to a binary and then use the file path... like we do for images and other stuff?

Ideas ...?

1st October 2008, 14:16
Based on the QT documentation, QFile can read from embedded resource, so you can build the info.txt into the exe and it can be opened and read from the application. Probably you cannot write it.
Nevertheless if you know the path of your application directory, you can create the absolute filepath of info.txt (QFileInfo) that can be used instead of its relative path.

1st October 2008, 16:36
Relative path is relative to current working directory not to the directory in which the executable is stored. If you wish to locate file in path based on executable's directory, you must use something like

QFile file(QCoreApplication::applicationDirPath()+"/relative/path");