Boost :: FileSystem이 존재합니다 ()는 디렉토리 경로에 실패하지만 is_directory ()는 괜찮습니다.

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

  •  18-09-2019
  •  | 
  •  

문제

Boost FileSystem을 사용하여 현재 디렉토리로가는 길을 얻은 다음 디렉토리가 있는지 확인합니다.

is_directory() 괜찮지 만 exists() 같은 길에서 실패하면 뭔가 빠졌나요?

예제 코드 (부스트 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

편집하다:

#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

재미있는 부분 :

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

하지만:

if (!fs::exists(fs::status(data_dir)))      // false - directory still exists
if (!fs::exists( data_dir ))                // true  - directory does not exists
도움이 되었습니까?

해결책

다음은 Boost 소스 코드입니다.

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; }

보시다시피 is_directory 그렇다면 진실을 반환합니다 exists 사실을 반환해야합니다. 문제가 코드의 다른 곳에있을 수도 있습니다. 문제를 보여주는 최소한의 편집 가능한 예제를 게시하십시오.

당신은 또한 같은 것을 사용해 보시기를 원할 수도 있습니다 file_status 출력이 있을지 확인하려면 두 통화 모두 객체 status 변화하고 있었다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top