35657

Untitled

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