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

int main()
{
        struct stat *buf = NULL;
        buf = (struct stat *)malloc(sizeof(struct stat));

        if(stat("123.txt", buf) == -1)
        {
                perror("stat error!\n");
                exit(1);
        }

        printf("file type               :%d\n", buf->st_mode);
        printf("i-node num              :%d\n", buf->st_ino);
        printf("dev num(firstsystem)    :%d\n", buf->st_dev);
        printf("dev num(special)        :%d\n", buf->st_rdev);
        printf("link numbers            :%d\n", buf->st_nlink);
        printf("uid                     :%d\n", buf->st_uid);
        printf("gid                     :%d\n", buf->st_gid);
        printf("uid                     :%d\n", buf->st_uid);
        printf("gid                     :%d\n", buf->st_gid);
        printf("size in bytes           :%d\n", buf->st_size);
        printf("last access time        :%d\n", buf->st_atime);
        printf("last modification time  :%d\n", buf->st_mtime);
        printf("last status change time :%d\n", buf->st_ctime);
        printf("best i/o block size     :%d\n", buf->st_blksize);
        printf("num of 512 blocks       :%d\n", buf->st_blocks);

        return 0;
}


stat函数和stat命令还是有很大的区别 ,比如stat命令获得的access时间是“Access: 2008-07-31 16:35:24.000000000 +0800”这种字样的时间,而stat函数获得的时间是“last access time        :1217493324”个样子的。我想可能是他们使用了struct tm *gmtime(const time_t *t)这个函数了吧 。这个我并没有测试。