stat函数使用

stat函数使用要点 通过 stat 函数获取文件属性代码示例 运行结果 a outa outatime mtime ctime a outxyz outErroroccu

stat是非常常用的一个函数,可以获取文件的一些属性,或判断文件的类型。

下面直接取自man 2 stat中的示例代码:

#include <sys/types.h> #include <sys/stat.h> #include <time.h> #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { struct stat sb; if (argc != 2) { fprintf(stderr, "Usage: %s <pathname>\n", argv[0]); exit(EXIT_FAILURE); } if (stat(argv[1], &sb) == -1) { perror("stat"); exit(EXIT_FAILURE); } printf("File type: "); switch (sb.st_mode & S_IFMT) { case S_IFBLK: printf("block device\n"); break; case S_IFCHR: printf("character device\n"); break; case S_IFDIR: printf("directory\n"); break; case S_IFIFO: printf("FIFO/pipe\n"); break; case S_IFLNK: printf("symlink\n"); break; case S_IFREG: printf("regular file\n"); break; case S_IFSOCK: printf("socket\n"); break; default: printf("unknown?\n"); break; } printf("I-node number: %ld\n", (long) sb.st_ino); printf("Mode: %lo (octal)\n", (unsigned long) sb.st_mode); printf("Link count: %ld\n", (long) sb.st_nlink); printf("Ownership: UID=%ld GID=%ld\n", (long) sb.st_uid, (long) sb.st_gid); printf("Preferred I/O block size: %ld bytes\n", (long) sb.st_blksize); printf("File size: %lld bytes\n", (long long) sb.st_size); printf("Blocks allocated: %lld\n", (long long) sb.st_blocks); printf("Last status change: %s", ctime(&sb.st_ctime)); printf("Last file access: %s", ctime(&sb.st_atime)); printf("Last file modification: %s", ctime(&sb.st_mtime)); exit(EXIT_SUCCESS); }

flying-bird@flyingbird:~/examples/cpp/stat$ ./a.out Usage: ./a.out <pathname> flying-bird@flyingbird:~/examples/cpp/stat$ ./a.out . File type: directory I-node number:  Mode: 40775 (octal) Link count: 2 Ownership: UID=1000 GID=1000 Preferred I/O block size: 4096 bytes File size: 4096 bytes Blocks allocated: 8 Last status change: Tue May 27 19:44:20 2014 Last file access: Tue May 27 19:44:21 2014 Last file modification: Tue May 27 19:44:20 2014 flying-bird@flyingbird:~/examples/cpp/stat$ ./a.out ./a.out File type: regular file I-node number:  Mode:  (octal) Link count: 1 Ownership: UID=1000 GID=1000 Preferred I/O block size: 4096 bytes File size: 7463 bytes Blocks allocated: 16 Last status change: Tue May 27 19:44:20 2014 Last file access: Tue May 27 19:44:22 2014 Last file modification: Tue May 27 19:44:20 2014 flying-bird@flyingbird:~/examples/cpp/stat$ ll /dev/sd sda sda1 sda2 sda3 sda5 sda6 sda7 sdb sdb1 sdb2 sdb5 sdb6 sdb7 sdc sdd sde sdf flying-bird@flyingbird:~/examples/cpp/stat$ ./a.out /dev/sda File type: block device I-node number: 6645 Mode: 60660 (octal) Link count: 1 Ownership: UID=0 GID=6 Preferred I/O block size: 4096 bytes File size: 0 bytes Blocks allocated: 0 Last status change: Tue May 27 18:41:12 2014 Last file access: Tue May 27 18:41:13 2014 Last file modification: Tue May 27 18:41:12 2014 flying-bird@flyingbird:~/examples/cpp/stat$ ./a.out /dev/sda5 File type: block device I-node number: 461 Mode: 60660 (octal) Link count: 1 Ownership: UID=0 GID=6 Preferred I/O block size: 4096 bytes File size: 0 bytes Blocks allocated: 0 Last status change: Tue May 27 18:41:13 2014 Last file access: Tue May 27 18:55:36 2014 Last file modification: Tue May 27 18:41:13 2014 flying-bird@flyingbird:~/examples/cpp/stat$ 






版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/233298.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • php中的<?= ?>和<?php ?>有什么区别么?

    php中的<?= ?>和<?php ?>有什么区别么?

    2022年2月8日
    42
  • oracle删除主键索引的sql语句_oracle主键索引和普通索引

    oracle删除主键索引的sql语句_oracle主键索引和普通索引–根据索引名,查询表索引字段select*fromuser_ind_columnswhereindex_name=’索引名’;–根据表名,查询一张表的索引select*fromuser_indexeswheretable_name=’表名’;–根据索引名,查询属于哪张表select*fromall_indexeswhereindex_name=’IN…

    2022年9月5日
    6
  • C语言输出有颜色的字体

    C语言输出有颜色的字体先看下面的一段代码:#include<stdio.h>intmain(intargc,char**argv){printf(“\033[44;37;5mhelloworld\033[0m\n”);return0;}编译后运行上述代码,结果如下:可见,此时输出的字体和背景已经有了颜色。由上可知,在输出时候加上“\033[…

    2022年7月24日
    24
  • 北京十大it外包公司

    北京十大it外包公司1.博朗软件Bleum(上海)2.中软国际(北京)3.东软集团Neusoft(沈阳)4.博彦科技BeyondSoft(北京)5.图灵方舟www.tlio.cn(河北)6.海辉软件HiSoft(大连)7.文思VanceInfo(北京)8.浙大网新Insigma(杭州)9.奥博杰天Objectiva(北京)10.浪潮Inspur(济南)…

    2022年6月10日
    121
  • webstorm2021激活码【注册码】

    webstorm2021激活码【注册码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月19日
    56
  • java 调用webservice (asmx) 客户端开发示例

    java 调用webservice (asmx) 客户端开发示例这是本人第一次写博客,其实就是自己做个笔记,也希望能给跟我遇到同样问题的你一点帮助。有不专业的地方敬请指正。    因为最近有个项目要调用webservice接口,之前接触的都是java开发服务端和客户端的接口,开发前服务端一般会给个以http://xxx/services?wsdl地址的文件或者地址。这次对方给我个http://xxxx/service/getinfo.asmx 的

    2022年5月31日
    81

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号