Click here to Skip to main content
14,391,651 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi,
I wanted to read the file "Logfile_P_1.txt" in my application.but I want to get the full path of the file"LogFile_P_1.txt". what is the best way to get the relative path of the File in QT::Dir? because I need to test my application on Linux and some time on Windows.

this is how i am doing
QDir dir("../MyProjects/ProjectB");
QString LFName;
LFName= dir.relativeFilePath("/LogFiles/Logfile_P_1.txt");
 QFile logfile(sdir);

but getting errors."File not found"
QFile logfile(LFName);
if (!logfile.open(QIODevice::ReadOnly | QIODevice::Text))
{
    qDebug()<<"File not found";
       return false;
}
qDebug()<<"File Found";

My file(Logfile_P_1.txt) absolute path is
C:\MyDevelopment\MyProjects\ProjectB\LogFiles\Logfile_P_1.txt
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 2

This is a relative path:
QDir dir("../MyProjects/ProjectB");

If the current working directory is not a sub directory of "C:\MyDevelopment\", the path will be probably invalid (not existing).

This will return the path to the passed name relative to the above directory.
LFName= dir.relativeFilePath("/LogFiles/Logfile_P_1.txt");

There are two problems here:

  1. The passed name begins with a slash. So this is not interpreted as sub path but as absolute path and the returned path may be "../../LogFiles/Logfile_P_1.txt".
  2. When omitting the leading slash, the return value is just the passed name ("LogFiles/Logfile_P_1.txt")

This is probably not what you want and won't work anyway when the initial path is wrong.


The usual solution for these kinds of problems is to define an application setting for the log file path stored in the registry (Windows) or a configuration file (Linux). Qt provides a class to store such settings.

The initial pathes should be application specific folders (usually the application name) with a sub directory (here "LogFiles") below the home directory (Linux) and the user document folder (Windows):
/home/<User Name>/<App Name>/LogFiles
C:\Users\<User Name>\Documents\<App Name>\LogFiles


Optionally provide a configuration option that allows changing the default pathes.
   
Comments
XamBEE 18-Jan-16 10:57am
   
you means initial path would be something like below..
QDir dir("C:\Users\userXAM\MyProjects\ProjectB\LogFiles");
and the rest would be.....
LFName= dir.relativeFilePath("Logfile_P_1.txt");
QFile logfile(sdir);
QFile logfile(LFName);
if (!logfile.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug()<<"File not found";
return false;
}
qDebug()<<"File Found";
Jochen Arndt 18-Jan-16 11:31am
   
Yes. The initial path must be an absolute one. This can be stored in a configuration setting.

When using relative pathes to access files, these will be relative to the current working directory. So you must set that first using chdir() or QDir::setCurrent:

QDir::setCurrent(dir.absolutePath());

Or just use always absolute path names by building them from the base path, an optional sub directory, and the file name.
XamBEE 18-Jan-16 12:24pm
   
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100