如何查找文件系统块大小?

mat*_*_nz 6 c filesystems gcc

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

谢谢

Shi*_*zou 15

关于你使用gcc编译器的信息并不有意思,因为编译器对文件系统的块大小不感兴趣,他们甚至不知道文件系统可以存在的事实......答案是系统特定的(MS Windows?GNU)/Linux或其他*nix/*nix如OS?); 在POSIX上你有这个stat功能,你可以使用它来获得stat结构,它包含st_blksize你可能感兴趣的字段(文件系统I/O的blocksize).

#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;
}
Run Code Online (Sandbox Code Playgroud)

告诉你关于/(root)上使用的文件系统; 例如,对我来说,它输出4096.