2017-02-09 4 views
0

사용자 입력으로부터 경로를 확인하고 싶습니다. 입력은 존재하지 않는 파일의 경로를 제공해야합니다. 또한 입력에서 디렉토리도 제공하는 경우 이러한 디렉토리가 있어야합니다.boost :: filesystem :: path.parent_path()와 공백

예 입력 :

/existent_dir_a/existent_dir_b/existent_file.bin < - 확인

/non_existent_dir_a/non_existent_file.bin < - - 거짓

non_existent_file.bin < - 확인

012

/existent_dir_a/existent_dir_b/non_existent_file.bin < 거짓< - 거짓

아래 코드는 예제입니다. 나는 그것이 내 목표를 달성하기를 희망하지만 나는 여전히 하나의 질문을 가지고있다.

output.parent_path().string().size() != 0을 어떻게 제거 할 수 있습니까?

#include <boost/filesystem.hpp> 
#include <iostream> 
#include <string> 
#include <exception> 

namespace fs = boost::filesystem; 

int main(int ac, char ** av) 
{ 
    fs::path output(av[1]); 

    std::cout << output << std::endl; 

    std::cout << output.parent_path() << std::endl; 

    if (fs::exists(output)) 
    { 
    std::string msg = output.string() + " already exists"; 

    throw std::invalid_argument(msg); 
    } 

    if (output.parent_path().string().size() != 0 && 
     !fs::exists(output.parent_path())) 
    { 
    std::string msg = output.parent_path().string() + " is not a directory"; 

    throw std::invalid_argument(msg); 
    } 
} 

답변

5

사용 !output.parent_path().empty()

관련 문제