Namespaces
Variants

std::filesystem::directory_entry:: assign

From cppreference.net
void assign ( const std:: filesystem :: path & p ) ;
(1) (C++17 이후)
void assign ( const std:: filesystem :: path & p, std:: error_code & ec ) ;
(2) (C++17 이후)

디렉토리 엔트리 객체에 새로운 내용을 할당합니다. 경로를 p 로 설정하고 캐시된 속성을 업데이트하기 위해 refresh 를 호출합니다. 오류가 발생하면 캐시된 속성들의 값은 지정되지 않습니다.

이 함수는 파일 시스템에 어떠한 변경 사항도 커밋하지 않습니다.

목차

매개변수

p - 디렉터리 항목이 참조할 파일시스템 객체의 경로
ec - 비예외 발생 오버로드에서 오류 보고를 위한 출력 매개변수

반환값

(없음)

예외

noexcept 로 표시되지 않은 모든 오버로드는 메모리 할당이 실패할 경우 std::bad_alloc 을 throw할 수 있습니다.

1) 기본 OS API 오류 발생 시 std::filesystem::filesystem_error 를 발생시킵니다. 이는 p 를 첫 번째 경로 인수로, OS 오류 코드를 오류 코드 인수로 구성되어 생성됩니다.
2) OS API 호출이 실패할 경우 std:: error_code & 매개변수를 OS API 오류 코드로 설정하고, 오류가 발생하지 않을 경우 ec. clear ( ) 를 실행합니다.

예제

#include <filesystem>
#include <fstream>
#include <iostream>
void print_entry_info(const std::filesystem::directory_entry& entry)
{
    if (std::cout << "The entry " << entry; not entry.exists())
    {
        std::cout << " does not exists on the file system\n";
        return;
    }
    std::cout << " is ";
    if (entry.is_directory())
        std::cout << "a directory\n";
    if (entry.is_regular_file())
        std::cout << "a regular file\n";
    /*...*/
}
int main()
{
    std::filesystem::current_path(std::filesystem::temp_directory_path());
    std::filesystem::directory_entry entry{std::filesystem::current_path()};
    print_entry_info(entry);
    std::filesystem::path name{"cppreference.html"};
    std::ofstream{name} << "C++";
    std::cout << "entry.assign();\n";
    entry.assign(entry/name);
    print_entry_info(entry);
    std::cout << "remove(entry);\n";
    std::filesystem::remove(entry);
    print_entry_info(entry); // the entry still contains old "state"
    std::cout << "entry.assign();\n";
    entry.assign(entry); // or just call entry.refresh()
    print_entry_info(entry);
}

가능한 출력:

The entry "/tmp" is a directory
entry.assign();
The entry "/tmp/cppreference.html" is a regular file
remove(entry);
The entry "/tmp/cppreference.html" is a regular file
entry.assign();
The entry "/tmp/cppreference.html" does not exists on the file system

참고 항목

내용 할당
(public member function)