代码如下: #include <linux/fs.h> #include <fcntl.h> #include <stdio.h> static unsigned long get_BLKGETSIZE_sectors(int fd) { uint64_t v64; unsigned long longsectors; if (ioctl(fd, BLKGETSIZE64, &v64) == 0) { /* Got bytes, convert to 512 byte sectors */ return (v64 >> 9); } /* Needs temp of type long */ if (ioctl(fd, BLKGETSIZE, &longsectors)) longsectors = 0; return longsectors; } int main( int argc, char *argv[] ) { int fd; unsigned long sectors; fd = open("/dev/mmcblk0", O_RDONLY); if(fd == -1) { printf("main: open sd card failed/n"); return -1; } sectors = get_BLKGETSIZE_sectors(fd); printf("sectors is %ld/n", sectors); return 0; }