Advertisement
35657

Untitled

Apr 27th, 2024
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2.  
  3. #include <iostream>
  4. #include <Windows.h>
  5. #include <fstream>
  6. #include <filesystem>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. // функция для вывода содержимого папки
  12. void print_directory(const string& str) {
  13.     for (const auto& a : filesystem::directory_iterator(str)) {
  14.         cout << a.path().filename().string() << endl;
  15.     }
  16. }
  17.  
  18.  
  19. int main() {
  20.     SetConsoleCP(1251);
  21.     SetConsoleOutputCP(1251);
  22.  
  23.     //создание пустой директории
  24.     filesystem::create_directory("C:/Users/PC/Desktop/temp");
  25.  
  26.  
  27.     //создание директории с подкаталогами
  28.     filesystem::create_directories("C:/Users/PC/Desktop/temp/temp2/1/2/3/4/5");
  29.  
  30.     ofstream fout("C:/Users/PC/Desktop/temp/file.txt");
  31.     ofstream fout2("C:/Users/PC/Desktop/temp/temp2/file2.txt");
  32.     ofstream fout3("C:/Users/PC/Desktop/temp/temp2/1/file3.txt");
  33.     ofstream fout4("C:/Users/PC/Desktop/temp/temp2/1/2/file4.txt");
  34.  
  35.  
  36.     //удаление пустой папки с помощью remove
  37.     filesystem::remove("C:/Users/PC/Desktop/temp/temp2/1/2/3/4/5");
  38.  
  39.     fout4.close();
  40.     //удаление файла с помощью remove (не забываем закрыть файл)
  41.     filesystem::remove("C:/Users/PC/Desktop/temp/temp2/1/2/file4.txt");
  42.  
  43.  
  44.     //переименование пустой папки
  45.     filesystem::rename("C:/Users/PC/Desktop/temp/temp2/1/2/3/4", "C:/Users/PC/Desktop/temp/temp2/1/2/3/5");
  46.  
  47.  
  48.     fout.close();
  49.     fout2.close();
  50.     fout3.close();
  51.     //переименование папки с каталогами и файлами (не забываем закрыть файлы)
  52.     filesystem::rename("C:/Users/PC/Desktop/temp", "C:/Users/PC/Desktop/temp33");
  53.  
  54.  
  55.     //копирование файла
  56.     filesystem::copy("C:/Users/PC/Desktop/temp33/temp2/1/file3.txt", "C:/Users/PC/Desktop/file3.txt");
  57.     //копирования папки с файлами но без вложенных каталогов
  58.     filesystem::copy("C:/Users/PC/Desktop/temp33", "C:/Users/PC/Desktop/temp34");
  59.     //копирование папки с файлами и вложенными каталогами
  60.     filesystem::copy("C:/Users/PC/Desktop/temp33", "C:/Users/PC/Desktop/temp34", filesystem::copy_options::recursive);
  61.  
  62.     //удаление папки с подкаталогами и файлами
  63.     filesystem::remove_all("C:/Users/PC/Desktop/temp34");
  64.  
  65.  
  66.     //определить размер файла
  67.     cout << filesystem::file_size("C:/Users/PC/Desktop/file3.txt") << endl;
  68.     //определить является ли объект файлом
  69.     cout << filesystem::is_regular_file("C:/Users/PC/Desktop/file3.txt") << endl;
  70.     //определить является ли объект папкой
  71.     cout << filesystem::is_directory("C:/Users/PC/Desktop/temp33") << endl;
  72.  
  73.     //вывести содержимое папки
  74.     print_directory("C:/Users/PC/Desktop");
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement