Spec-Zone.ru › C++

std::filesystem::directory_entry::assign

void assign( const std::filesystem::path& p );
(1) (since C++17)
void assign( const std::filesystem::path& p, std::error_code& ec );
(2) (since C++17)

Присваивает новое содержимое объекту записи каталога. Устанавливает путь к p и вызывает refresh для обновления кэшированных атрибутов. Если произошла ошибка, значения кэшированных атрибутов не определены.

Эта функция не производит никаких изменений в файловой системе.

Параметры

p - путь к объекту файловой системы, на который будет ссылаться запись каталога
ec - параметр вывода для сообщения об ошибках в перегрузке без исключений

Возвращаемое значение

(нет)

Исключения

Любая перегрузка, не помеченная noexcept может выбросить std::bad_alloc при сбое выделения памяти.

1) Выбрасывает std::filesystem::filesystem_error при ошибках в базовых API операционной системы, созданных с p в качестве первого аргумента пути и кодом ошибки ОС в качестве аргумента ошибки.
2) Устанавливает параметр std::error_code& в код ошибки API ОС, если вызов 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

См. также

operator=
присваивает содержимое
(общедоступная функция-член)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/filesystem/directory_entry/assign

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API