35657

Untitled

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