LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware
User Name
Password
Linux - Hardware This forum is for Hardware issues.
Having trouble installing a piece of hardware? Want to know if that peripheral is compatible with Linux?

Notices

Reply
 
Search this Thread
Old 06-24-2009, 04:36 AM   #1
dwijesh
LQ Newbie
 
Registered: Apr 2009
Posts: 8

Rep: Reputation: 0
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
 
Old 06-24-2009, 09:41 PM   #2
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,405

Rep: Reputation: 2529Reputation: 2529Reputation: 2529Reputation: 2529Reputation: 2529Reputation: 2529Reputation: 2529Reputation: 2529Reputation: 2529Reputation: 2529Reputation: 2529
Quote:
Originally Posted by dwijesh View Post
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.
 
Old 06-24-2009, 11:27 PM   #3
dwijesh
LQ Newbie
 
Registered: Apr 2009
Posts: 8

Original Poster
Rep: Reputation: 0
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:isplay(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:iskSpace()
{
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....
 
Old 06-25-2009, 08:32 AM   #4
r0b0
Member
 
Registered: Aug 2004
Location: Europe
Posts: 602

Rep: Reputation: 49
man 2 statfs

http://linux.die.net/man/2/statfs
 
Old 06-26-2009, 01:35 AM   #5
dwijesh
LQ Newbie
 
Registered: Apr 2009
Posts: 8

Original Poster
Rep: Reputation: 0
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hard disk partitioning/I am out of space /how to increase linux space? RMLinux Red Hat 8 09-05-2008 12:33 PM
creating mount points on the unused Hard disk after installation of RedHat Linux ES 4 vamsikatakam Linux - Enterprise 4 03-21-2007 12:55 PM
Why does the hard disc space get full till the very end? suro Linux - Software 3 07-11-2005 11:09 AM
array size/C++ memory limits for user space programs? thrifteed Programming 4 09-05-2004 04:42 AM
hard disc space (and samba?) milesstandish Linux - General 5 11-27-2002 10:43 PM


All times are GMT -5. The time now is 11:37 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration