Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include <iostream>
- #include <string>
- #include <fstream>
- #include <filesystem>
- #include <windows.h>
- using namespace std;
- using filesystem::path;
- // вывод содержимого директории (просто название файлов и каталогов)
- void PrintDirectory(const string& str) { // указываем полный путь к папке
- for (const auto& a : filesystem::directory_iterator(str)) {
- cout << a.path().filename().string() << endl;
- }
- }
- int main() {
- SetConsoleCP(1251); // установка кодировки 1251 в поток ввода
- SetConsoleOutputCP(1251); // установка кодировки 1251 в поток вывода
- filesystem::create_directory("C:/Users/PC/Desktop/temp"); // создание папки
- filesystem::create_directories("C:/Users/PC/Desktop/temp/temp2/1/2/3/4/5"); // создание папки с подпапками
- ofstream fout("C:/Users/PC/Desktop/temp/file.txt"); // создание файла (можно совмещать создание объекта (ofstream fout) и открытие файла (fout.open("file.txt"))
- ofstream fout2("C:/Users/PC/Desktop/temp/temp2/file2.txt");
- ofstream fout3("C:/Users/PC/Desktop/temp/temp2/1/file3.txt");
- ofstream fout4("C:/Users/PC/Desktop/temp/temp2/1/2/file4.txt");
- filesystem::remove("C:/Users/PC/Desktop/temp/temp2/1/2/3/4/5"); // удалить пустую папку с помощью remove из std::filesystem
- fout4.close();
- filesystem::remove("C:/Users/PC/Desktop/temp/temp2/1/2/file4.txt"); // удалить файл с помощью remove из std::filesystem (не забыть его закрыть)
- 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
- fout3.close();
- fout2.close();
- fout.close();
- filesystem::rename("C:/Users/PC/Desktop/temp", "C:/Users/PC/Desktop/temp33"); // переименовать папку с помощью rename из std::filesystem (не забыть закрыть все файлы) (можно также переименовать с перемещением или просто переместить без переименования)
- filesystem::copy("C:/Users/PC/Desktop/temp33/temp2/1/file3.txt", "C:/Users/PC/Desktop/file3.txt"); // копирование файла
- filesystem::copy("C:/Users/PC/Desktop/temp33", "C:/Users/PC/Desktop/temp34"); // копирование папки с файлами но без подкаталогов
- filesystem::copy("C:/Users/PC/Desktop/temp33", "C:/Users/PC/Desktop/temp35", filesystem::copy_options::recursive); // копирование папки с подкаталогами
- filesystem::remove_all("C:/Users/PC/Desktop/temp35"); // удалить непустую папку и все содержимое с помощью remove_all из std::filesystem (не забыть закрыть все файлы)
- cout << filesystem::file_size("C:/Users/PC/Desktop/file3.txt") << endl; // возвращает размер файла (для папки нужно делать рекурсивный обход всех её подкаталогов)
- cout << filesystem::is_regular_file("C:/Users/PC/Desktop/file3.txt") << endl; // выяснить является ли объект файлом
- cout << filesystem::is_directory("C:/Users/PC/Desktop/file3.txt") << endl; // выяснить является ли объект папкой
- cout << endl << endl;
- PrintDirectory("C:/Users/PC/Desktop"); // вывести содержимое папки (названия каталогов и файлов)
- }
Advertisement
Add Comment
Please, Sign In to add comment