cpp_filesystem - 8BitsCoding/RobotMentor GitHub Wiki

cpp_filesystem

์‚ฌ์šฉ๋ฒ•์ด๋‹ˆ ํ•„์š”ํ•˜๋‹ค๋ฉด ๊ฒ€์ƒ‰ํ•ด์„œ ์‚ฌ์šฉํ•˜์ž!

  • C++17์˜ ์ƒˆ๋กœ์šด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
  • C++14๋‚˜ ๊ทธ ์ „์—๋Š” ํŒŒ์ผ ์‹œ์Šคํ…œ๊ณผ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ตฌ์„ฑ์š”์†Œ์— ๋Œ€ํ•ด ์—ฐ์‚ฐ์„ ํ•  ๋ฐฉ๋ฒ•์ด ์—†์—ˆ์Œ
    • ๊ฒฝ๋กœ
    • ์ผ๋ฐ˜ ํŒŒ์ผ
    • ๋””๋ ‰ํ„ฐ๋ฆฌ
  • ํŒŒ์ผ ์ฝ๊ธฐ์™€ ์“ฐ๊ธฐ์— ๊ด€ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๊ฐ€ ์•„๋‹˜
  • ํŒŒ์ผ ์†์„ฑ ๋ณ€๊ฒฝ, ๋””๋ ‰ํ„ฐ๋ฆฌ ์ˆœํšŒ, ํŒŒ์ผ ๋ณต์‚ฌ ๋“ฑ์— ๊ด€ํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ
  • ์ด ๋ชจ๋“  ๊ฑธ std::fs๋กœ ํ•  ์ˆ˜ ์žˆ์Œ.

ํŒŒ์ผ ์‹œ์Šคํ…œ ๊ธฐ๋Šฅ?

  • ํ”Œ๋žซํผ ๊ณตํ†ต์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ฒฝ๋กœ ํ•ฉ์น˜๊ธฐ
  • ํŒŒ์ผ๊ณผ ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ณต์‚ฌ, ์ด๋ฆ„ ๋ฐ”๊พธ๊ธฐ, ์‚ญ์ œ
  • ๋””๋ ‰ํ„ฐ๋ฆฌ์—์„œ ํŒŒ์ผ, ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ
  • ํŒŒ์ผ ๊ถŒํ•œ ์ฝ๊ธฐ ๋ฐ ์„ค์ •
  • ํŒŒ์ผ ์ƒํƒœ ์ฝ๊ธฐ ๋ฐ ์„ค์ •

Example

#include <filesystem>

namespace fs = std::experimental::filesystem::v1;
// ์ปดํŒŒ์ผ๋Ÿฌ์— ๋”ฐ๋ผ std::filesystem์ผ ์ˆ˜ ์žˆ์Œ.

int main() {
    fs::path path1 = "D:\\Lecture";
    fs::path path2 = "examples";
    path /= path2;
    // D:\\Lecture\\examples

    fs::path path3 = "D:\\Lecture";
    fs::path path4 = "exampels";
    path3 += path4;
    // D:\\Lectureexamples

    fs::path path5 = "D:\\Lecture";
    fs::path path6 = "\exampels";
    path5 /= path6;
    // D:\\exampels

    return 0;
}

cppreference์—์„œ ๋‹ค๋ฅธ ์˜ˆ์ œ ์ฐธ๊ณ 


ํŒŒ์ผ ๋˜๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ณต์‚ฌ

#include <filesystem>

namespace fs = std::experimental::filesystem::v1;

int main() {
    fs::path originalTextPath = "C:\\exampels\\myRank.txt";
    fs::path copiedTextPath = "C:\\exampels\\copiedMyRank.txt";

    fs::path originalDirPath = "C:\\examples\\folder1";
    fs::path copiedDirPath1 = "C:\\examples\\copiedfolder1";
    fs::path copiedDirPath2 = "C:\\examples\\copiedfolder2";

    fs::copy(originalTextPath, copiedTextPath);     
    // ํŒŒ์ผ ๋ณต์‚ฌ
    fs::copy(originalDirPath, copiedDirPath1);     
    // ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ณต์‚ฌ (๋น„์žฌ๊ท€)
    fs::copy(originalDirPath, copiedDirPath2, fs::copy_option::recursive);     
    // ๋””๋ ‰ํ„ฐ๋ฆฌ ๋ณต์‚ฌ (์žฌ๊ท€)

    return 0;
}

์ด๋ฆ„ ๋ฐ”๊พธ๊ธฐ ๋ฐ ์ด๋™

#include <experimental/filesystem>

namespace fs = std::experimental::filesystem::v1;

int main() {
    fs::path filePath = "C:\\exampels\\myRank.txt";
    fs::path renamedPath = "C:\\exampels\\folder1\\rank.txt";

    fs::rename(filePath, renamedPath);

    return 0;
}
  • ํŒŒ์ผ ๋˜๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ์˜ ์ด๋ฆ„์„ ๋ฐ”๊พธ๊ฑฐ๋‚˜ ์ด๋™์‹œํ‚จ๋‹ค.

ํŒŒ์ผ ๋˜๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ ์‚ญ์ œ

#include <experimental/filesystem>

namespace fs = std::experimental::filesystem::v1;

int main() {
    fs::path currentPath = fs::current_path();
    fs::create_directories(currentPath / "data");
    // ํ”„๋กœ์ ํŠธ ํด๋”์— ์ƒˆ ํด๋”๋ฅผ ๋งŒ๋“ฆ
    fs::remove(currentPath / "data");

    return 0;
}

๋””๋ ‰ํ„ฐ๋ฆฌ ํŒŒ์ผ ๋ชฉ๋ก ๊ฐ€์ ธ์˜ค๊ธฐ

#include <experimental/filesystem>

namespace fs = std::experimental::filesystem::v1;

int main() {
    for(auto& path : fs::recursive_directory_iterator("C:\\Lecture\\FilesysystemExample"))
    {
        std::cout << path << std::endl;
    }

    return 0;
}

ํŒŒ์ผ ์†์„ฑ(๊ถŒํ•œ) ์ฝ๊ธฐ

#include <experimental/filesystem>

namespace fs = std::experimental::filesystem::v1;

void PrintPermission(fs::perms permission)
{
    std::cout << ((permission & fs::perms::owner_read) != fs::perms::none ? "r" : "-")
    // ...
    // ๋งŽ์œผ๋‹ˆ ๊ฒ€์ƒ‰ํ•ด ๋ณผ ๊ฒƒ
}

int main() {
    fs::path filePath = "C:\\examples\\file.txt";
    PrintPermission(fs::status(filePath).permissions());
}
โš ๏ธ **GitHub.com Fallback** โš ๏ธ