tags:

views:

190

answers:

3

I wanted to know a way to find out wich is the disk's block size through a function or a compiler constant in C..

thanks

+5  A: 

The info about you using gcc compiler is not interesting, since compilers are not interested in the block size of the filesystem, they are not even aware of the fact that a filesystem can exist... the answer is system specific (MS Windows? GNU/Linux or other *nix/*nix like OS?); on POSIX you have the stat function, you can use it to have the stat struct, which contains the field st_blksize (blocksize for filesystem I/O) which could be what you're interested in.

ADD

Example

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>


int main()
{
  struct stat fi;
  stat("/", &fi);
  printf("%d\n", fi.st_blksize);
  return 0;
}

Tells you about the filesystem used on / (root); e.g. for me, it outputs 4096.

ShinTakezou
So in linux I could use this struct? I'm very sorry I'm a begginer. Could you use it in a example?
maty_nz
start reading e.g. http://linux.die.net/man/2/stat to see if it is what you want to know
ShinTakezou
Ok thanks i was really needeing it to complete a school project about structures on disk(tree b+, hashing, etc) thanks
maty_nz
+1  A: 

statvfs() reports on a filesystem. stat() reports on a given file. Almost always this is going to be the same, but since you asked for the result from a filesystem the correct answer for POSIX systems is to call statvfs().

jim mcnamara
not wrong at all, but I wonder when those values don't match
ShinTakezou
fstyp -v /dev/vx/dsk/r1sp1dbdg/sapdata31vxfsversion: 5f_bsize: 8192f_frsize: 1024
jim mcnamara
I could not get the output above to format correctly. Sorry. - HPUX vxfs may show that.
jim mcnamara
A: 

I found another way to solve my problem.

There is a function at gcc that returns the page size.

I solved my problem using getpagesize()

printf("%d\n",getpagesize());
maty_nz