简体   繁体   中英

Bug in boost::filesystem?

This code correctly gets contents of a directory specified in selected_paths but only if the directory is "C:". If the directory is "D:" this code iterates over a root directory (the directory where the source files are located - "D:\\excercizes\\QT_projects\\my_app") of my app. What's going on?

   QStringList my_app::extract_files_from_paths_(const QStringList& selected_paths)const
{
    boost::filesystem3::path path;
    QStringList result;
    for (auto e : selected_paths)
    {
       boost::filesystem3::path path(e.toStdString().c_str());
       if (boost::filesystem3::is_regular_file(path))
       {
           result.append(e);
       }
       else if (boost::filesystem3::is_directory(path) && !boost::filesystem3::is_empty(path))
       {
        std::vector<boost::filesystem3::path> paths_;
        /*add everything from this path*/
           std::copy(boost::filesystem3::directory_iterator(path), boost::filesystem3::directory_iterator(), // directory_iterator::value_type
                     std::back_inserter(paths_));
           QStringList list_of_files;
           for(auto e : paths_)
           {
               list_of_files.append(QString(e.string().c_str()));
           }
               return extract_files_from_paths_(list_of_files);

       }

    }
    return result;
}

D: and D:\\ are two different things on Windows.

  • D:\\ designates the root of the D drive
  • D: designates the current directory on the D drive

One current directory is stored per drive (per process). So it's not a boost bug, its a Windows feature.

In a cmd shell, you can see the current directory for a drive with (eg):

cd d:

You can change it by specifying a path:

cd d:\home

(note that this will not change your current working directory if you're not on D .)

cd /dd: and cd /dd:\\ will respectively change the shell's working directory to the current directory of D , and to the root of D .

It's not a bug. C: resolves to the current directory on drive C , which happens to be C:\\ . D: resolves to the current directory on drive D , which happens to be D:\\excercizes\\QT_projects\\my_app . You shall write D:\\ to list the files in the root of drive D .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM