How can I get a file's size in C++ in 2019 with C++ 17?How can I get a file's size in C++?tellg() function give wrong size of file?Fstream's tellg / seekg returning higher value than expectedHow can I determine the current size of the file opened by std::ofstream?how to get the filesize for large files in c++What are some resources for getting started in operating system development?How can I safely create a nested directory?How can I profile C++ code running on Linux?What is an application binary interface (ABI)?What are the basic rules and idioms for operator overloading?How can I get a file's size in C++?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Image Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionWhen should I really use noexcept?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviations

Installed software from source, how to say yum not to install it from package?

What does this Pokemon Trainer mean by saying the player is "SHELLOS"?

What is the point of using the kunai?

Bootstrap paradox with a time machine in iron

Can you help me, to widen the page. Thank you

Which high-degree derivatives play an essential role?

Searching for single buildings in QGIS

What caused the flashes in the video footage of Chernobyl?

What's the idiomatic (or best) way to trim surrounding whitespace from a string?

Odd PCB Layout for Voltage Regulator

What verb goes with "coup"?

When does it become illegal to exchange bitcoin for cash?

Non-inverting amplifier ; Single supply ; Bipolar input

Does "boire un jus" tend to mean "coffee" or "juice of fruit"?

Can I deep fry food in butter instead of vegetable oil?

Other homotopy invariants?

What could a Medieval society do with excess animal blood?

How can solar sailed ships be protected from space debris?

How can I know (without going to the station) if RATP is offering the Anti Pollution tickets?

"Best practices" for formulating MIPs

Why would Dementors torture a Death Eater if they are loyal to Voldemort?

How to model a Coral or Sponge Structure?

To “Er” Is Human

Could citing a database like libgen get one into trouble?



How can I get a file's size in C++ in 2019 with C++ 17?


How can I get a file's size in C++?tellg() function give wrong size of file?Fstream's tellg / seekg returning higher value than expectedHow can I determine the current size of the file opened by std::ofstream?how to get the filesize for large files in c++What are some resources for getting started in operating system development?How can I safely create a nested directory?How can I profile C++ code running on Linux?What is an application binary interface (ABI)?What are the basic rules and idioms for operator overloading?How can I get a file's size in C++?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?Image Processing: Algorithm Improvement for 'Coca-Cola Can' RecognitionWhen should I really use noexcept?Replacing a 32-bit loop counter with 64-bit introduces crazy performance deviations













7















How can I get a file's size in C++ with modern technologies as they are available in 2019 with C++ 17?



Are there pitfalls for specific operating systems, I should know of?



There are many duplicates (1, 2, 3, 4, 5) of this question but they were answered decades ago. The very high voted answers in many of these questions are wrong today.










share|improve this question



















  • 3





    Starter for 10: en.cppreference.com/w/cpp/header/filesystem

    – Richard Critten
    8 hours ago











  • Possible duplicate of How can I determine the current size of the file opened by std::ofstream?

    – Ted Lyngmo
    7 hours ago











  • How, exactly, do those answers go wrong?

    – L. F.
    3 hours ago















7















How can I get a file's size in C++ with modern technologies as they are available in 2019 with C++ 17?



Are there pitfalls for specific operating systems, I should know of?



There are many duplicates (1, 2, 3, 4, 5) of this question but they were answered decades ago. The very high voted answers in many of these questions are wrong today.










share|improve this question



















  • 3





    Starter for 10: en.cppreference.com/w/cpp/header/filesystem

    – Richard Critten
    8 hours ago











  • Possible duplicate of How can I determine the current size of the file opened by std::ofstream?

    – Ted Lyngmo
    7 hours ago











  • How, exactly, do those answers go wrong?

    – L. F.
    3 hours ago













7












7








7


2






How can I get a file's size in C++ with modern technologies as they are available in 2019 with C++ 17?



Are there pitfalls for specific operating systems, I should know of?



There are many duplicates (1, 2, 3, 4, 5) of this question but they were answered decades ago. The very high voted answers in many of these questions are wrong today.










share|improve this question
















How can I get a file's size in C++ with modern technologies as they are available in 2019 with C++ 17?



Are there pitfalls for specific operating systems, I should know of?



There are many duplicates (1, 2, 3, 4, 5) of this question but they were answered decades ago. The very high voted answers in many of these questions are wrong today.







c++ operating-system c++17 filesize






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 7 hours ago







Jonas Stein

















asked 8 hours ago









Jonas SteinJonas Stein

2,4435 gold badges27 silver badges56 bronze badges




2,4435 gold badges27 silver badges56 bronze badges







  • 3





    Starter for 10: en.cppreference.com/w/cpp/header/filesystem

    – Richard Critten
    8 hours ago











  • Possible duplicate of How can I determine the current size of the file opened by std::ofstream?

    – Ted Lyngmo
    7 hours ago











  • How, exactly, do those answers go wrong?

    – L. F.
    3 hours ago












  • 3





    Starter for 10: en.cppreference.com/w/cpp/header/filesystem

    – Richard Critten
    8 hours ago











  • Possible duplicate of How can I determine the current size of the file opened by std::ofstream?

    – Ted Lyngmo
    7 hours ago











  • How, exactly, do those answers go wrong?

    – L. F.
    3 hours ago







3




3





Starter for 10: en.cppreference.com/w/cpp/header/filesystem

– Richard Critten
8 hours ago





Starter for 10: en.cppreference.com/w/cpp/header/filesystem

– Richard Critten
8 hours ago













Possible duplicate of How can I determine the current size of the file opened by std::ofstream?

– Ted Lyngmo
7 hours ago





Possible duplicate of How can I determine the current size of the file opened by std::ofstream?

– Ted Lyngmo
7 hours ago













How, exactly, do those answers go wrong?

– L. F.
3 hours ago





How, exactly, do those answers go wrong?

– L. F.
3 hours ago










2 Answers
2






active

oldest

votes


















11














<filesystem> (added in C++17) makes this very straightforward.



#include <cstdint>
#include <filesystem>

// ...

std::uintmax_t size = std::filesystem::file_size("c:foobar.txt");





share|improve this answer


















  • 2





    Little offtopic: is there a world where std::uintmax_t will be able to hold greater values than std::size_t? If not, why not use std::size_t, which arguably is more recognisable? +1 on the answer, btw

    – Fureeish
    8 hours ago






  • 3





    @Fureeish I used just because that's the type file_size returns. Looks slightly weird to me too.

    – HolyBlackCat
    8 hours ago







  • 3





    @Fureeish std::size_t is only required to hold the max size of in memory objects. Files can be considerably larger,

    – Richard Critten
    8 hours ago







  • 1





    @RichardCritten so the answer to the first question of my comment is "yes"?

    – Fureeish
    8 hours ago






  • 4





    @Fureeish Well, on 32-bit Windows (and I assume on most modern 32-bit platforms) size_t is 32 bits, and uintmax_t is 64 bits.

    – HolyBlackCat
    8 hours ago



















7














This might help.



#include <chrono>
#include <filesystem>
#include <iostream>

namespace fs = std::filesystem;

int main(int argc, char* argv[])

try

const auto fsize = fs::file_size("a.out");
std::cout << fsize << 'n';

catch (const fs::filesystem_error& err)

std::cerr << "filesystem error! " << err.what() << 'n';
if (!err.path1().empty())
std::cerr << "path1: " << err.path1().string() << 'n';
if (!err.path2().empty())
std::cerr << "path2: " << err.path2().string() << 'n';

catch (const std::exception& ex)

std::cerr << "general exception: " << ex.what() << 'n';


// using error_code
std::error_code ec;
auto size = std::filesystem::file_size("a.out", ec);
if (ec == std::error_code)
std::cout << "size: " << size << 'n';
else
std::cout << "error when accessing test file, size is: "
<< size << " message: " << ec.message() << 'n';






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56827878%2fhow-can-i-get-a-files-size-in-c-in-2019-with-c-17%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    11














    <filesystem> (added in C++17) makes this very straightforward.



    #include <cstdint>
    #include <filesystem>

    // ...

    std::uintmax_t size = std::filesystem::file_size("c:foobar.txt");





    share|improve this answer


















    • 2





      Little offtopic: is there a world where std::uintmax_t will be able to hold greater values than std::size_t? If not, why not use std::size_t, which arguably is more recognisable? +1 on the answer, btw

      – Fureeish
      8 hours ago






    • 3





      @Fureeish I used just because that's the type file_size returns. Looks slightly weird to me too.

      – HolyBlackCat
      8 hours ago







    • 3





      @Fureeish std::size_t is only required to hold the max size of in memory objects. Files can be considerably larger,

      – Richard Critten
      8 hours ago







    • 1





      @RichardCritten so the answer to the first question of my comment is "yes"?

      – Fureeish
      8 hours ago






    • 4





      @Fureeish Well, on 32-bit Windows (and I assume on most modern 32-bit platforms) size_t is 32 bits, and uintmax_t is 64 bits.

      – HolyBlackCat
      8 hours ago
















    11














    <filesystem> (added in C++17) makes this very straightforward.



    #include <cstdint>
    #include <filesystem>

    // ...

    std::uintmax_t size = std::filesystem::file_size("c:foobar.txt");





    share|improve this answer


















    • 2





      Little offtopic: is there a world where std::uintmax_t will be able to hold greater values than std::size_t? If not, why not use std::size_t, which arguably is more recognisable? +1 on the answer, btw

      – Fureeish
      8 hours ago






    • 3





      @Fureeish I used just because that's the type file_size returns. Looks slightly weird to me too.

      – HolyBlackCat
      8 hours ago







    • 3





      @Fureeish std::size_t is only required to hold the max size of in memory objects. Files can be considerably larger,

      – Richard Critten
      8 hours ago







    • 1





      @RichardCritten so the answer to the first question of my comment is "yes"?

      – Fureeish
      8 hours ago






    • 4





      @Fureeish Well, on 32-bit Windows (and I assume on most modern 32-bit platforms) size_t is 32 bits, and uintmax_t is 64 bits.

      – HolyBlackCat
      8 hours ago














    11












    11








    11







    <filesystem> (added in C++17) makes this very straightforward.



    #include <cstdint>
    #include <filesystem>

    // ...

    std::uintmax_t size = std::filesystem::file_size("c:foobar.txt");





    share|improve this answer













    <filesystem> (added in C++17) makes this very straightforward.



    #include <cstdint>
    #include <filesystem>

    // ...

    std::uintmax_t size = std::filesystem::file_size("c:foobar.txt");






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered 8 hours ago









    HolyBlackCatHolyBlackCat

    18.5k3 gold badges37 silver badges71 bronze badges




    18.5k3 gold badges37 silver badges71 bronze badges







    • 2





      Little offtopic: is there a world where std::uintmax_t will be able to hold greater values than std::size_t? If not, why not use std::size_t, which arguably is more recognisable? +1 on the answer, btw

      – Fureeish
      8 hours ago






    • 3





      @Fureeish I used just because that's the type file_size returns. Looks slightly weird to me too.

      – HolyBlackCat
      8 hours ago







    • 3





      @Fureeish std::size_t is only required to hold the max size of in memory objects. Files can be considerably larger,

      – Richard Critten
      8 hours ago







    • 1





      @RichardCritten so the answer to the first question of my comment is "yes"?

      – Fureeish
      8 hours ago






    • 4





      @Fureeish Well, on 32-bit Windows (and I assume on most modern 32-bit platforms) size_t is 32 bits, and uintmax_t is 64 bits.

      – HolyBlackCat
      8 hours ago













    • 2





      Little offtopic: is there a world where std::uintmax_t will be able to hold greater values than std::size_t? If not, why not use std::size_t, which arguably is more recognisable? +1 on the answer, btw

      – Fureeish
      8 hours ago






    • 3





      @Fureeish I used just because that's the type file_size returns. Looks slightly weird to me too.

      – HolyBlackCat
      8 hours ago







    • 3





      @Fureeish std::size_t is only required to hold the max size of in memory objects. Files can be considerably larger,

      – Richard Critten
      8 hours ago







    • 1





      @RichardCritten so the answer to the first question of my comment is "yes"?

      – Fureeish
      8 hours ago






    • 4





      @Fureeish Well, on 32-bit Windows (and I assume on most modern 32-bit platforms) size_t is 32 bits, and uintmax_t is 64 bits.

      – HolyBlackCat
      8 hours ago








    2




    2





    Little offtopic: is there a world where std::uintmax_t will be able to hold greater values than std::size_t? If not, why not use std::size_t, which arguably is more recognisable? +1 on the answer, btw

    – Fureeish
    8 hours ago





    Little offtopic: is there a world where std::uintmax_t will be able to hold greater values than std::size_t? If not, why not use std::size_t, which arguably is more recognisable? +1 on the answer, btw

    – Fureeish
    8 hours ago




    3




    3





    @Fureeish I used just because that's the type file_size returns. Looks slightly weird to me too.

    – HolyBlackCat
    8 hours ago






    @Fureeish I used just because that's the type file_size returns. Looks slightly weird to me too.

    – HolyBlackCat
    8 hours ago





    3




    3





    @Fureeish std::size_t is only required to hold the max size of in memory objects. Files can be considerably larger,

    – Richard Critten
    8 hours ago






    @Fureeish std::size_t is only required to hold the max size of in memory objects. Files can be considerably larger,

    – Richard Critten
    8 hours ago





    1




    1





    @RichardCritten so the answer to the first question of my comment is "yes"?

    – Fureeish
    8 hours ago





    @RichardCritten so the answer to the first question of my comment is "yes"?

    – Fureeish
    8 hours ago




    4




    4





    @Fureeish Well, on 32-bit Windows (and I assume on most modern 32-bit platforms) size_t is 32 bits, and uintmax_t is 64 bits.

    – HolyBlackCat
    8 hours ago






    @Fureeish Well, on 32-bit Windows (and I assume on most modern 32-bit platforms) size_t is 32 bits, and uintmax_t is 64 bits.

    – HolyBlackCat
    8 hours ago












    7














    This might help.



    #include <chrono>
    #include <filesystem>
    #include <iostream>

    namespace fs = std::filesystem;

    int main(int argc, char* argv[])

    try

    const auto fsize = fs::file_size("a.out");
    std::cout << fsize << 'n';

    catch (const fs::filesystem_error& err)

    std::cerr << "filesystem error! " << err.what() << 'n';
    if (!err.path1().empty())
    std::cerr << "path1: " << err.path1().string() << 'n';
    if (!err.path2().empty())
    std::cerr << "path2: " << err.path2().string() << 'n';

    catch (const std::exception& ex)

    std::cerr << "general exception: " << ex.what() << 'n';


    // using error_code
    std::error_code ec;
    auto size = std::filesystem::file_size("a.out", ec);
    if (ec == std::error_code)
    std::cout << "size: " << size << 'n';
    else
    std::cout << "error when accessing test file, size is: "
    << size << " message: " << ec.message() << 'n';






    share|improve this answer



























      7














      This might help.



      #include <chrono>
      #include <filesystem>
      #include <iostream>

      namespace fs = std::filesystem;

      int main(int argc, char* argv[])

      try

      const auto fsize = fs::file_size("a.out");
      std::cout << fsize << 'n';

      catch (const fs::filesystem_error& err)

      std::cerr << "filesystem error! " << err.what() << 'n';
      if (!err.path1().empty())
      std::cerr << "path1: " << err.path1().string() << 'n';
      if (!err.path2().empty())
      std::cerr << "path2: " << err.path2().string() << 'n';

      catch (const std::exception& ex)

      std::cerr << "general exception: " << ex.what() << 'n';


      // using error_code
      std::error_code ec;
      auto size = std::filesystem::file_size("a.out", ec);
      if (ec == std::error_code)
      std::cout << "size: " << size << 'n';
      else
      std::cout << "error when accessing test file, size is: "
      << size << " message: " << ec.message() << 'n';






      share|improve this answer

























        7












        7








        7







        This might help.



        #include <chrono>
        #include <filesystem>
        #include <iostream>

        namespace fs = std::filesystem;

        int main(int argc, char* argv[])

        try

        const auto fsize = fs::file_size("a.out");
        std::cout << fsize << 'n';

        catch (const fs::filesystem_error& err)

        std::cerr << "filesystem error! " << err.what() << 'n';
        if (!err.path1().empty())
        std::cerr << "path1: " << err.path1().string() << 'n';
        if (!err.path2().empty())
        std::cerr << "path2: " << err.path2().string() << 'n';

        catch (const std::exception& ex)

        std::cerr << "general exception: " << ex.what() << 'n';


        // using error_code
        std::error_code ec;
        auto size = std::filesystem::file_size("a.out", ec);
        if (ec == std::error_code)
        std::cout << "size: " << size << 'n';
        else
        std::cout << "error when accessing test file, size is: "
        << size << " message: " << ec.message() << 'n';






        share|improve this answer













        This might help.



        #include <chrono>
        #include <filesystem>
        #include <iostream>

        namespace fs = std::filesystem;

        int main(int argc, char* argv[])

        try

        const auto fsize = fs::file_size("a.out");
        std::cout << fsize << 'n';

        catch (const fs::filesystem_error& err)

        std::cerr << "filesystem error! " << err.what() << 'n';
        if (!err.path1().empty())
        std::cerr << "path1: " << err.path1().string() << 'n';
        if (!err.path2().empty())
        std::cerr << "path2: " << err.path2().string() << 'n';

        catch (const std::exception& ex)

        std::cerr << "general exception: " << ex.what() << 'n';


        // using error_code
        std::error_code ec;
        auto size = std::filesystem::file_size("a.out", ec);
        if (ec == std::error_code)
        std::cout << "size: " << size << 'n';
        else
        std::cout << "error when accessing test file, size is: "
        << size << " message: " << ec.message() << 'n';







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 8 hours ago









        GOVIND DIXITGOVIND DIXIT

        1651 silver badge13 bronze badges




        1651 silver badge13 bronze badges



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56827878%2fhow-can-i-get-a-files-size-in-c-in-2019-with-c-17%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

            Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

            199年 目錄 大件事 到箇年出世嗰人 到箇年死嗰人 節慶、風俗習慣 導覽選單