Get HDD size programatically on MacOS using C++











up vote
0
down vote

favorite












I am building an application and I need to get the HDD size:
I am working on Mac OS. I have tried this:



#include <sys/param.h>
#include <sys/mount.h>

float MonitoringWorker::getHDDSize()
{
struct statfs statf;

statfs(".", &statf);

std::cout << "statf.f_bsize = " << statf.f_bsize << std::endl;
std::cout << "statf.f_blocks = " << statf.f_blocks << std::endl;
std::cout << "statf.f_bavail = " << statf.f_bavail << std::endl;
std::cout << "statf.f_bfree = "<< statf.f_bfree << std::endl;
std::cout << "GB = "<< ((statf.f_bsize * statf.f_blocks) / kBytesInGB)<< std::endl;
return 0;

}


I am seeing that the result in GB is 465. However checking System Information tells me that I have 500GB
What am I doing wrong? Is this best way to get those numbers?



Note: I am using C++ on Mac OS, can't use Objective-C



Thanks in advance










share|improve this question




















  • 1




    The program gets the file system capacity, not the physical capacity of the HDD.
    – eprom
    2 days ago










  • how can I get the physical capacity, then?
    – RuLoViC
    2 days ago















up vote
0
down vote

favorite












I am building an application and I need to get the HDD size:
I am working on Mac OS. I have tried this:



#include <sys/param.h>
#include <sys/mount.h>

float MonitoringWorker::getHDDSize()
{
struct statfs statf;

statfs(".", &statf);

std::cout << "statf.f_bsize = " << statf.f_bsize << std::endl;
std::cout << "statf.f_blocks = " << statf.f_blocks << std::endl;
std::cout << "statf.f_bavail = " << statf.f_bavail << std::endl;
std::cout << "statf.f_bfree = "<< statf.f_bfree << std::endl;
std::cout << "GB = "<< ((statf.f_bsize * statf.f_blocks) / kBytesInGB)<< std::endl;
return 0;

}


I am seeing that the result in GB is 465. However checking System Information tells me that I have 500GB
What am I doing wrong? Is this best way to get those numbers?



Note: I am using C++ on Mac OS, can't use Objective-C



Thanks in advance










share|improve this question




















  • 1




    The program gets the file system capacity, not the physical capacity of the HDD.
    – eprom
    2 days ago










  • how can I get the physical capacity, then?
    – RuLoViC
    2 days ago













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am building an application and I need to get the HDD size:
I am working on Mac OS. I have tried this:



#include <sys/param.h>
#include <sys/mount.h>

float MonitoringWorker::getHDDSize()
{
struct statfs statf;

statfs(".", &statf);

std::cout << "statf.f_bsize = " << statf.f_bsize << std::endl;
std::cout << "statf.f_blocks = " << statf.f_blocks << std::endl;
std::cout << "statf.f_bavail = " << statf.f_bavail << std::endl;
std::cout << "statf.f_bfree = "<< statf.f_bfree << std::endl;
std::cout << "GB = "<< ((statf.f_bsize * statf.f_blocks) / kBytesInGB)<< std::endl;
return 0;

}


I am seeing that the result in GB is 465. However checking System Information tells me that I have 500GB
What am I doing wrong? Is this best way to get those numbers?



Note: I am using C++ on Mac OS, can't use Objective-C



Thanks in advance










share|improve this question















I am building an application and I need to get the HDD size:
I am working on Mac OS. I have tried this:



#include <sys/param.h>
#include <sys/mount.h>

float MonitoringWorker::getHDDSize()
{
struct statfs statf;

statfs(".", &statf);

std::cout << "statf.f_bsize = " << statf.f_bsize << std::endl;
std::cout << "statf.f_blocks = " << statf.f_blocks << std::endl;
std::cout << "statf.f_bavail = " << statf.f_bavail << std::endl;
std::cout << "statf.f_bfree = "<< statf.f_bfree << std::endl;
std::cout << "GB = "<< ((statf.f_bsize * statf.f_blocks) / kBytesInGB)<< std::endl;
return 0;

}


I am seeing that the result in GB is 465. However checking System Information tells me that I have 500GB
What am I doing wrong? Is this best way to get those numbers?



Note: I am using C++ on Mac OS, can't use Objective-C



Thanks in advance







macos c++11 filesystems






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 16:55









Tom Harrington

51.5k596126




51.5k596126










asked Nov 10 at 11:59









RuLoViC

15512




15512








  • 1




    The program gets the file system capacity, not the physical capacity of the HDD.
    – eprom
    2 days ago










  • how can I get the physical capacity, then?
    – RuLoViC
    2 days ago














  • 1




    The program gets the file system capacity, not the physical capacity of the HDD.
    – eprom
    2 days ago










  • how can I get the physical capacity, then?
    – RuLoViC
    2 days ago








1




1




The program gets the file system capacity, not the physical capacity of the HDD.
– eprom
2 days ago




The program gets the file system capacity, not the physical capacity of the HDD.
– eprom
2 days ago












how can I get the physical capacity, then?
– RuLoViC
2 days ago




how can I get the physical capacity, then?
– RuLoViC
2 days ago

















active

oldest

votes











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',
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%2f53238719%2fget-hdd-size-programatically-on-macos-using-c%23new-answer', 'question_page');
}
);

Post as a guest





































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238719%2fget-hdd-size-programatically-on-macos-using-c%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation