如何从绝对路径走向相对路径?

时间:2014-02-17 06:39:18

标签: c++ qt relative-path absolute-path

我正在制作一个小程序来帮助文件阅读。

我有一个for循环从命令行接收命令:

 for (i = 1; argc > i; i++)
            {
                QString path = QDir::currentPath()+ "/" + QString(argv[i]);
                QDir dir(path);
                fileSearch(dir);
            }

从那里我调用另一种方法,我查看每个文件/ foler并获取大小以及它是文件还是文件夹。

void fileSearch(QDir dir)
{
        foreach(QFileInfo info, dir.entryInfoList(QDir::NoDotAndDotDot | QDir::Files | QDir::AllDirs ))
            {
                if (info.isFile())
                {
                    qDebug() << info.path() << "is a file! its size is:" << info.size() << endl;
                }
                if (info.isDir())
                {
                    qDebug() << info.path() << "is a directory! its size is:" << info.size() << endl;
                    fileSearch(info.filePath());
                }
            }

而不是读取整个路径,我希望它只读取相对路径。所以不要阅读:

home/john/desktop/project/currentproject/checkdirectory is a directory! its size is: 4096
home/john/desktop/project/currentproject/checkdirectory/test.txt is a file! its size is: 4

我想要阅读:

checkdirectory/ is a directory! its size is: 4096
checkdirectory/test.txt is a file! its size is: 4

1 个答案:

答案 0 :(得分:5)

QString QDir::relativeFilePath(const QString & fileName);

应该返回相对文件路径。