简体   繁体   中英

How to find the filesystem block size?

我想知道一种通过 C 中的函数或编译器常量找出磁盘块大小的方法。

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); eg for me, it outputs 4096.

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().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM