how to check free room of the disk?

I want to know the free room of disk under QNX,now in my project I need
saving data to disk every second,I have to check if the disk is full and delete the lastest file,but I don’t know use which function,I try to use “opendir” and “statvfs”,but can’t find the information I need,please tell me how to do,thanks.

fstatvfs()/fstatvfs64()

sir,Could you tell me which parameter in fstatvfs() has relation with my question and how to use it correctly?I read the explain of fstatvfs(),the help says that I must give one fd for the first parameter of that function,for example,if I want to know the free space of the disk,I have to open one file,that sounds a little strange,please tell me how to do.

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/statvfs.h>

int main (void)
{
int filedes;
struct statvfs temp;

filedes = open("/",O_RDONLY);
fstatvfs(filedes, &temp);

printf("All blocks: %i, Block size %i, Free block: %i\n",temp.f_blocks,temp.f_bsize,temp.f_bfree);

}

That will give you a starting block.

Cheers

Garry

sir,I do just as you say above,the output is “all block:1 ,block size:1,free block:0”,but I want to know the exact size(byte) of free space of my disk.

Did you check that fstatvfs succeeded ?

If it failed, then the members of temp would just contain stack garbage.

And make sure ‘open’ succeeded, very unlikely you cannot open ‘/’ for reading, but hey, you never know.

the fstatvfs() return 0,that means it success.

the open() return success

I wonder if the open/fstatvfs calls are working, but printf is not, maybe try %l rather than %i in the printf, or do an if statement to see if the number returned for say, free blocks is over 1.

This should work better, you can’t use ‘/’. Looks like / isn’t the mount point for a file system (makes sense)

#include <fcntl.h>
#include <stdio.h>
#include <sys/statvfs.h>

int main (void)
{
int filedes;
struct statvfs temp;

// — or use any other filename that’s on the device you want to check
filedes = open("/tmp",O_RDONLY);
if ( filedes < 0 )
{
perror(“Cannot open directory”);
return -1;
}

if ( fstatvfs(filedes, &temp) != 0 )
{
	perrorf("Can't get fstast");	
}

printf("All blocks: %d, Block size %ld, Free block: %d\n",temp.f_blocks,temp.f_bsize,temp.f_bfree);

return 0;

}

It’s very strange - program above shows different values with the df utility:
Program above (in kilobytes):
Capacity: 2048000 kB, Root free: 787968 kB, User free: 787968 kB, Used: 62%
df:
$ df -k /home
/dev/hd0t79 2048287 1260204 788083 62% /

Will anybody tell me, how can it be and how to get the same result in my own program?