How do I programmatically get the free disk space for a directory in Linux

后端 未结 4 2089
失恋的感觉
失恋的感觉 2020-12-23 19:42

Is there a function that returns how much space is free on a drive partition given a directory path?

4条回答
  •  滥情空心
    2020-12-23 20:19

    With C++17

    You can use std::filesystem::space:

    #include   // only needed for screen output
    
    #include 
    namespace fs = std::filesystem;
    
    int main()
    {
        fs::space_info tmp = fs::space("/tmp");
    
        std::cout << "Free space: " << tmp.free << '\n'
                  << "Available space: " << tmp.available << '\n';
    }
    

提交回复
热议问题