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...
Turing Machines: What is the difference between recognizing, deciding, total, accepting, rejecting?
Disk usage confusion: 10G missing on Linux home partition on SSD
Available snapshots for main net?
How can solar sailed ships be protected from space debris?
Cannot overlay, because ListPlot does not draw same X range despite the same PlotRange
Does the Grothendieck group of finitely generated modules form a commutative ring where the multiplication structure is induced from tensor product?
Which are more efficient in putting out wildfires: planes or helicopters?
How soon after takeoff can you recline your airplane seat?
Why will we fail creating a self sustaining off world colony?
How to extract coefficients of a generating function like this one, using a computer?
How can I write this expression using summation or double summation?
What happened to the Apollo 1 rocket?
ShellExView vs ShellMenuView
GFCI versus circuit breaker
What is the small microphone icon floating on top of the screen in macOS and how do I turn it off?
How can I know (without going to the station) if RATP is offering the Anti Pollution tickets?
Russian equivalents of 能骗就骗 (if you can cheat, then cheat)
How might boat designs change in order to allow them to be pulled by dragons?
Merging two data frames into a new one with unique items marked with 1 or 0
What was the ASCII end of medium (EM) character intended to be used for?
Why is the saxophone not common in classical repertoire?
Can you run PoE Cat6 alongside standard Cat6 cables?
Is it OK to say "The situation is pregnant with a crisis"?
Why would a propellor have blades of different lengths?
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
10 hours ago
Possible duplicate of How can I determine the current size of the file opened by std::ofstream?
– Ted Lyngmo
9 hours ago
How, exactly, do those answers go wrong?
– L. F.
5 hours ago
@L.F.: Well, the first question has been closed as a duplicate of the second, which explains why the accepted answer in the first is wrong. The third one is asking about similartellg
problems. The only one worth bothering with is the fourth one, and that one's not great, since it talks too much aboutofstream
, in both the question and its answers. This one is far better at expressing the intent than the others (except for the first, which is oddly closed).
– Nicol Bolas
1 hour 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 9 hours ago
Jonas Stein
asked 10 hours ago
Jonas SteinJonas Stein
2,4765 gold badges27 silver badges57 bronze badges
2,4765 gold badges27 silver badges57 bronze badges
3
Starter for 10: en.cppreference.com/w/cpp/header/filesystem
– Richard Critten
10 hours ago
Possible duplicate of How can I determine the current size of the file opened by std::ofstream?
– Ted Lyngmo
9 hours ago
How, exactly, do those answers go wrong?
– L. F.
5 hours ago
@L.F.: Well, the first question has been closed as a duplicate of the second, which explains why the accepted answer in the first is wrong. The third one is asking about similartellg
problems. The only one worth bothering with is the fourth one, and that one's not great, since it talks too much aboutofstream
, in both the question and its answers. This one is far better at expressing the intent than the others (except for the first, which is oddly closed).
– Nicol Bolas
1 hour ago
add a comment |
3
Starter for 10: en.cppreference.com/w/cpp/header/filesystem
– Richard Critten
10 hours ago
Possible duplicate of How can I determine the current size of the file opened by std::ofstream?
– Ted Lyngmo
9 hours ago
How, exactly, do those answers go wrong?
– L. F.
5 hours ago
@L.F.: Well, the first question has been closed as a duplicate of the second, which explains why the accepted answer in the first is wrong. The third one is asking about similartellg
problems. The only one worth bothering with is the fourth one, and that one's not great, since it talks too much aboutofstream
, in both the question and its answers. This one is far better at expressing the intent than the others (except for the first, which is oddly closed).
– Nicol Bolas
1 hour ago
3
3
Starter for 10: en.cppreference.com/w/cpp/header/filesystem
– Richard Critten
10 hours ago
Starter for 10: en.cppreference.com/w/cpp/header/filesystem
– Richard Critten
10 hours ago
Possible duplicate of How can I determine the current size of the file opened by std::ofstream?
– Ted Lyngmo
9 hours ago
Possible duplicate of How can I determine the current size of the file opened by std::ofstream?
– Ted Lyngmo
9 hours ago
How, exactly, do those answers go wrong?
– L. F.
5 hours ago
How, exactly, do those answers go wrong?
– L. F.
5 hours ago
@L.F.: Well, the first question has been closed as a duplicate of the second, which explains why the accepted answer in the first is wrong. The third one is asking about similar
tellg
problems. The only one worth bothering with is the fourth one, and that one's not great, since it talks too much about ofstream
, in both the question and its answers. This one is far better at expressing the intent than the others (except for the first, which is oddly closed).– Nicol Bolas
1 hour ago
@L.F.: Well, the first question has been closed as a duplicate of the second, which explains why the accepted answer in the first is wrong. The third one is asking about similar
tellg
problems. The only one worth bothering with is the fourth one, and that one's not great, since it talks too much about ofstream
, in both the question and its answers. This one is far better at expressing the intent than the others (except for the first, which is oddly closed).– Nicol Bolas
1 hour 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");
5
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
10 hours ago
6
@Fureeish I used just because that's the typefile_size
returns. Looks slightly weird to me too.
– HolyBlackCat
10 hours ago
8
@Fureeishstd::size_t
is only required to hold the max size of in memory objects. Files can be considerably larger,
– Richard Critten
10 hours ago
2
@RichardCritten so the answer to the first question of my comment is "yes"?
– Fureeish
10 hours ago
6
@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
10 hours ago
|
show 4 more comments
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");
5
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
10 hours ago
6
@Fureeish I used just because that's the typefile_size
returns. Looks slightly weird to me too.
– HolyBlackCat
10 hours ago
8
@Fureeishstd::size_t
is only required to hold the max size of in memory objects. Files can be considerably larger,
– Richard Critten
10 hours ago
2
@RichardCritten so the answer to the first question of my comment is "yes"?
– Fureeish
10 hours ago
6
@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
10 hours ago
|
show 4 more comments
<filesystem>
(added in C++17) makes this very straightforward.
#include <cstdint>
#include <filesystem>
// ...
std::uintmax_t size = std::filesystem::file_size("c:foobar.txt");
5
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
10 hours ago
6
@Fureeish I used just because that's the typefile_size
returns. Looks slightly weird to me too.
– HolyBlackCat
10 hours ago
8
@Fureeishstd::size_t
is only required to hold the max size of in memory objects. Files can be considerably larger,
– Richard Critten
10 hours ago
2
@RichardCritten so the answer to the first question of my comment is "yes"?
– Fureeish
10 hours ago
6
@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
10 hours ago
|
show 4 more comments
<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 10 hours ago
HolyBlackCatHolyBlackCat
18.6k3 gold badges37 silver badges71 bronze badges
18.6k3 gold badges37 silver badges71 bronze badges
5
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
10 hours ago
6
@Fureeish I used just because that's the typefile_size
returns. Looks slightly weird to me too.
– HolyBlackCat
10 hours ago
8
@Fureeishstd::size_t
is only required to hold the max size of in memory objects. Files can be considerably larger,
– Richard Critten
10 hours ago
2
@RichardCritten so the answer to the first question of my comment is "yes"?
– Fureeish
10 hours ago
6
@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
10 hours ago
|
show 4 more comments
5
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
10 hours ago
6
@Fureeish I used just because that's the typefile_size
returns. Looks slightly weird to me too.
– HolyBlackCat
10 hours ago
8
@Fureeishstd::size_t
is only required to hold the max size of in memory objects. Files can be considerably larger,
– Richard Critten
10 hours ago
2
@RichardCritten so the answer to the first question of my comment is "yes"?
– Fureeish
10 hours ago
6
@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
10 hours ago
5
5
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
10 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
10 hours ago
6
6
@Fureeish I used just because that's the type
file_size
returns. Looks slightly weird to me too.– HolyBlackCat
10 hours ago
@Fureeish I used just because that's the type
file_size
returns. Looks slightly weird to me too.– HolyBlackCat
10 hours ago
8
8
@Fureeish
std::size_t
is only required to hold the max size of in memory objects. Files can be considerably larger,– Richard Critten
10 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
10 hours ago
2
2
@RichardCritten so the answer to the first question of my comment is "yes"?
– Fureeish
10 hours ago
@RichardCritten so the answer to the first question of my comment is "yes"?
– Fureeish
10 hours ago
6
6
@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
10 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
10 hours ago
|
show 4 more comments
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 10 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
10 hours ago
Possible duplicate of How can I determine the current size of the file opened by std::ofstream?
– Ted Lyngmo
9 hours ago
How, exactly, do those answers go wrong?
– L. F.
5 hours ago
@L.F.: Well, the first question has been closed as a duplicate of the second, which explains why the accepted answer in the first is wrong. The third one is asking about similar
tellg
problems. The only one worth bothering with is the fourth one, and that one's not great, since it talks too much aboutofstream
, in both the question and its answers. This one is far better at expressing the intent than the others (except for the first, which is oddly closed).– Nicol Bolas
1 hour ago