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
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
add a comment |
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
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
add a comment |
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
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
c++ operating-system c++17 filesize
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
<filesystem>
(added in C++17) makes this very straightforward.
#include <cstdint>
#include <filesystem>
// ...
std::uintmax_t size = std::filesystem::file_size("c:foobar.txt");
2
Little offtopic: is there a world wherestd::uintmax_t
will be able to hold greater values thanstd::size_t
? If not, why not usestd::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 typefile_size
returns. Looks slightly weird to me too.
– HolyBlackCat
8 hours ago
3
@Fureeishstd::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, anduintmax_t
is 64 bits.
– HolyBlackCat
8 hours ago
|
show 1 more comment
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';
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
<filesystem>
(added in C++17) makes this very straightforward.
#include <cstdint>
#include <filesystem>
// ...
std::uintmax_t size = std::filesystem::file_size("c:foobar.txt");
2
Little offtopic: is there a world wherestd::uintmax_t
will be able to hold greater values thanstd::size_t
? If not, why not usestd::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 typefile_size
returns. Looks slightly weird to me too.
– HolyBlackCat
8 hours ago
3
@Fureeishstd::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, anduintmax_t
is 64 bits.
– HolyBlackCat
8 hours ago
|
show 1 more comment
<filesystem>
(added in C++17) makes this very straightforward.
#include <cstdint>
#include <filesystem>
// ...
std::uintmax_t size = std::filesystem::file_size("c:foobar.txt");
2
Little offtopic: is there a world wherestd::uintmax_t
will be able to hold greater values thanstd::size_t
? If not, why not usestd::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 typefile_size
returns. Looks slightly weird to me too.
– HolyBlackCat
8 hours ago
3
@Fureeishstd::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, anduintmax_t
is 64 bits.
– HolyBlackCat
8 hours ago
|
show 1 more comment
<filesystem>
(added in C++17) makes this very straightforward.
#include <cstdint>
#include <filesystem>
// ...
std::uintmax_t size = std::filesystem::file_size("c:foobar.txt");
<filesystem>
(added in C++17) makes this very straightforward.
#include <cstdint>
#include <filesystem>
// ...
std::uintmax_t size = std::filesystem::file_size("c:foobar.txt");
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 wherestd::uintmax_t
will be able to hold greater values thanstd::size_t
? If not, why not usestd::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 typefile_size
returns. Looks slightly weird to me too.
– HolyBlackCat
8 hours ago
3
@Fureeishstd::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, anduintmax_t
is 64 bits.
– HolyBlackCat
8 hours ago
|
show 1 more comment
2
Little offtopic: is there a world wherestd::uintmax_t
will be able to hold greater values thanstd::size_t
? If not, why not usestd::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 typefile_size
returns. Looks slightly weird to me too.
– HolyBlackCat
8 hours ago
3
@Fureeishstd::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, anduintmax_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
|
show 1 more comment
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';
add a comment |
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';
add a comment |
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';
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';
answered 8 hours ago
GOVIND DIXITGOVIND DIXIT
1651 silver badge13 bronze badges
1651 silver badge13 bronze badges
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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