LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Hardware (https://www.linuxquestions.org/questions/linux-hardware-18/)
-   -   How to get Hard Disc memory size used and unused space in c/c++ Linux program (https://www.linuxquestions.org/questions/linux-hardware-18/how-to-get-hard-disc-memory-size-used-and-unused-space-in-c-c-linux-program-735227/)

dwijesh 06-24-2009 04:36 AM

How to get Hard Disc memory size used and unused space in c/c++ Linux program
 
Hi all,

Any body kindly help me to find out the available and consumed Hard Disc memory through c/c++ program .

I am using Dabian linux 2.6
I am able to get the physical memory size information by reading /proc file

I need to get information of HDD memory throug c/c++

Any help !

Thanks in adv..

Dwijesh Maharana
dwijesma@gmail.com

TB0ne 06-24-2009 09:41 PM

Quote:

Originally Posted by dwijesh (Post 3584398)
Hi all,

Any body kindly help me to find out the available and consumed Hard Disc memory through c/c++ program .

I am using Dabian linux 2.6
I am able to get the physical memory size information by reading /proc file

I need to get information of HDD memory throug c/c++

Any help !

Thanks in adv..

Dwijesh Maharana
dwijesma@gmail.com

Easily done, and there are lots of examples out there on the web. Post what you've written so far, and we'll be glad to help you. But we're not going to do your homework for you.

dwijesh 06-24-2009 11:27 PM

I have tried like this but I don't want to use shell command through popen() system call

/*
* File: memoryInfo.cpp
* Author: root
*
* Created on 24 June, 2009, 6:32 AM
*/

#include <iostream>

using namespace std;
class MemoryInfo {
private:
char line[256];

public:
int GetFreeRamInKB(void);
int GetRamInKB(void);
void Display(int i);
int DiskSpace();
int TotalSizeofDisk();
};

int MemoryInfo::GetFreeRamInKB()
{
FILE *meminfo = fopen("/proc/meminfo", "r");
if(meminfo == NULL) printf("file opening error:");
// handle error

while(fgets(line, sizeof(line), meminfo))
{
int ram;
//if(sscanf(line, "MemTotal: %d kB", &ram) == 1)
if(sscanf(line, "MemFree: %d kB", &ram) == 1)
{
fclose(meminfo);
return ram;
}
}

fclose(meminfo);
return -1;
}

int MemoryInfo::GetRamInKB()
{
FILE *meminfo = fopen("/proc/meminfo", "r");
if(meminfo == NULL) printf("file opening error:");
// handle error

while(fgets(line, sizeof(line), meminfo))
{
int ram;
//if(sscanf(line, "MemTotal: %d kB", &ram) == 1)
if(sscanf(line, "MemTotal: %d kB", &ram) == 1)
{
fclose(meminfo);
return ram;
}
}

fclose(meminfo);
return -1;
}

void MemoryInfo::Display(int i)
{
cout << i << " KB" << endl;
}

int main() {

MemoryInfo RAM,HDD;
int freesize,Totalsize,Occupaied;
cout <<"\n\n RAM INFORMATION \n";
freesize =RAM.GetFreeRamInKB();
cout << "Free Memory space : ";
RAM.Display(freesize);
cout << "Total Memory space :";
Totalsize = RAM.GetRamInKB();
RAM.Display(Totalsize);

Occupaied = (Totalsize - freesize);
cout << "Occupaied memory :";
RAM.Display(Occupaied);

cout << "\n HARD DISK INFORMATION \n ";
HDD.DiskSpace();
HDD.TotalSizeofDisk();
return (EXIT_SUCCESS);
}

int MemoryInfo::DiskSpace()
{
FILE *fp;
// char line[200];
fp = popen( "df -H ", "r"); // df -H | grep /dev/sda

while ( fgets( line, sizeof line, fp))
{
printf("%s", line);
}
cout << endl;
pclose(fp);
}
int MemoryInfo::TotalSizeofDisk()
{
FILE *fp;
//char line[200];
fp = popen( "fdisk -l|grep Disk", "r");
while ( fgets( line, sizeof line, fp))
{
printf("Total %s", line);
}
cout << endl;
pclose(fp);
}


Dwijesh....

r0b0 06-25-2009 08:32 AM

man 2 statfs

http://linux.die.net/man/2/statfs

dwijesh 06-26-2009 01:35 AM

Hi,
Thanks for your reply I have tried by using statfs() but I could under stand about block side and free bock size is it the same result what I was getting by using "df -k" inside popen() . my program is

#include <sys/statfs.h>
#include <stdio.h>

//int statfs(const char *path, struct statfs *buf);
//int fstatfs(int fd, struct statfs *buf);

main() {
struct statfs info;

if (-1 == statfs("/dev/sda1", &info))
perror("statvfs() error");
else {
puts("statf returned the following information");
puts("about the Root ('/dev/sda1') file system:\n");
printf("size f_bsize : %u\n", info.f_bsize);
printf("used f_blocks : %ld\n",info.f_blocks);
printf("free f_bfree : %ld\n", info.f_bfree);

}
}

output:

statf returned the following information
about the Root ('/dev/sda1') file system:

size f_bsize : 4096
used f_blocks : 2560
free f_bfree : 2547


---------------
previously i was getting the size of hard disk by passing popen("df -kh","r")

output:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 228G 4.8G 212G 3% /
tmpfs 247M 0 247M 0% /lib/init/rw
udev 10M 52K 10M 1% /dev
tmpfs 247M 0 247M 0% /dev/shm


please kindly tell me what is the resemble and difference
how can i get like size :228G Used: 4.8G Avail : 212G


All times are GMT -5. The time now is 02:04 AM.