#include <iostream>
#include <filesystem>
#include <fstream>
#include <string>

namespace fs = std::filesystem;

int main() {
    // 1. Get the current working directory
    fs::path current = fs::current_path();
    std::cout << "Current path: " << current << std::endl;

    // 2. Create a test directory inside the current path
    fs::path test_dir = current / "test_filesystem";
    if (fs::exists(test_dir)) {
        std::cout << "Directory already exists: " << test_dir << std::endl;
    } else {
        if (fs::create_directory(test_dir)) {
            std::cout << "Created directory: " << test_dir << std::endl;
        } else {
            std::cerr << "Failed to create directory: " << test_dir << std::endl;
            return 1;
        }
    }

    // 3. Create some files in the test directory
    for (int i = 1; i <= 3; ++i) {
        fs::path file = test_dir / ("file" + std::to_string(i) + ".txt");
        std::ofstream ofs(file);
        if (ofs) {
            ofs << "This is file " << i << "\n";
            ofs.close();
            std::cout << "Created file: " << file.filename() << std::endl;
        } else {
            std::cerr << "Failed to create file: " << file << std::endl;
        }
    }

    // 4. List contents of the test directory (show name, type, and size)
    std::cout << "\nContents of " << test_dir << ":" << std::endl;
    for (const auto& entry : fs::directory_iterator(test_dir)) {
        fs::path p = entry.path();
        std::cout << "  " << p.filename();
        if (fs::is_regular_file(entry.status())) {
            std::cout << " (file, " << fs::file_size(p) << " bytes)";
        } else if (fs::is_directory(entry.status())) {
            std::cout << " (directory)";
        } else {
            std::cout << " (other)";
        }
        std::cout << std::endl;
    }

    // 5. Create a subdirectory and copy a file into it
    fs::path sub_dir = test_dir / "subdir";
    if (fs::create_directory(sub_dir)) {
        std::cout << "\nCreated subdirectory: " << sub_dir.filename() << std::endl;
        fs::path src = test_dir / "file1.txt";
        fs::path dst = sub_dir / "file1_copy.txt";
        try {
            fs::copy(src, dst, fs::copy_options::overwrite_existing);
            std::cout << "Copied " << src.filename() << " to " << dst.filename() << std::endl;
        } catch (const fs::filesystem_error& e) {
            std::cerr << "Copy failed: " << e.what() << std::endl;
        }
    } else {
        std::cerr << "Failed to create subdirectory: " << sub_dir << std::endl;
    }

    // 6. Rename a file
    fs::path old_name = test_dir / "file2.txt";
    fs::path new_name = test_dir / "renamed_file2.txt";
    try {
        fs::rename(old_name, new_name);
        std::cout << "Renamed " << old_name.filename() << " to " << new_name.filename() << std::endl;
    } catch (const fs::filesystem_error& e) {
        std::cerr << "Rename failed: " << e.what() << std::endl;
    }

    // 7. Remove a file
    fs::path to_remove = test_dir / "file3.txt";
    if (fs::remove(to_remove)) {
        std::cout << "Removed file: " << to_remove.filename() << std::endl;
    } else {
        std::cerr << "Failed to remove file: " << to_remove << std::endl;
    }

    // 8. (Optional) Remove the entire test directory and its contents
    // Uncomment the following lines with caution – they will delete all created files.
    /*
    std::cout << "\nRemoving test directory..." << std::endl;
    std::uintmax_t removed = fs::remove_all(test_dir);
    std::cout << "Removed " << removed << " files/directories" << std::endl;
    */

    std::cout << "\nFilesystem operations completed." << std::endl;
    return 0;
}

results matching ""

    No results matching ""