boost :: filesystem vorhanden ist () auf Verzeichnispfad ausfällt, aber is_directory () ist ok

StackOverflow https://stackoverflow.com/questions/2203919

  •  18-09-2019
  •  | 
  •  

Frage

Ich erhalte Pfad zum aktuellen Verzeichnis mit Boost-Dateisystem, dann überprüft, ob das Verzeichnis vorhanden ist.

is_directory() ist ok, aber exists() nicht auf dem gleichen Weg, bin ich etwas fehlt?

Beispiel-Code (Boost 1,35):

#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>

namespace fs = boost::filesystem;

// the path is full path /home/my/somedirectory    
fs::path data_dir(fs::current_path());

fs::is_directory(data_dir)  // true, directory exists

fs::exists(data_dir)   // false
exists(status(data_dir))  // false

EDIT:

#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
namespace fs = boost::filesystem;

fs::path data_dir(fs::current_path());
// data_dir == "/home/myusername/proj/test"

if (fs::is_directory(data_dir))             // true - is directory
if (fs::is_directory(fs::status(data_dir))) // true - it is still a directory

Spaß Teil:

if (fs::exists(fs::status(data_dir)))       // true - directory exists
if (fs::exists( data_dir ))                 // true - directory exists

ABER:

if (!fs::exists(fs::status(data_dir)))      // false - directory still exists
if (!fs::exists( data_dir ))                // true  - directory does not exists
War es hilfreich?

Lösung

Hier finden Sie von dem Boost-Quellcode:

inline bool is_directory( file_status f ) { return f.type() == directory_file; }
inline bool exists( file_status f )       { return f.type() != status_unknown && f.type() != file_not_found; }

Wie Sie sehen können, wenn is_directory true zurück, dann muss exists true zurück als auch. Vielleicht ist das Problem an andere Stelle in Ihrem Code - bitte ein minimales übersetzbar Beispiel schreiben, das das Problem zeigt.

Sie können auch für beiden Anrufe mit dem gleichen file_status Objekt versuchen wollen, um zu sehen, ob vielleicht die Ausgabe status wurde ändern.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top