PDA

View Full Version : File path issue...



kaushal_gaurav
1st October 2008, 09:39
Hi,

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.....

GK

spirit
1st October 2008, 10:01
maybe this will help


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

???

kaushal_gaurav
1st October 2008, 10: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.

aamer4yu
1st October 2008, 10: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

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


application<dir>
|-----res<dir>
| |---info.txt<file>
|-----application.exe<file>

right?

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

or

application<dir>
|-----res<dir>
| |---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.

spirit
1st October 2008, 10: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.

kaushal_gaurav
1st October 2008, 11: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 ...?

brazso
1st October 2008, 13: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.

martyone
1st October 2008, 15: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");