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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • PB AcceptText()函数

    PB AcceptText()函数文章来源:https://bbs.csdn.net/topics/380140428此文章解释的很清楚,以下博文仅用于记录AcceptText()功能将“漂浮”在数据窗口控件上编辑框的内容放入到数据窗口控件的当前项中(主缓区中)。在将数据放入到当前项之前,编辑框中的数据必须通过有效性规则检查语法dwcontrol.AcceptText()参数dwcontrol:数…

    2022年7月26日
    12
  • 教你如何快速将网站开发为桌面应用

    教你如何快速将网站开发为桌面应用

    2021年9月18日
    59
  • mysql/mariadb 忘记root密码处理

    mysql/mariadb 忘记root密码处理

    2021年5月14日
    128
  • xshell怎么连接vmware虚拟机_虚拟机centos7怎么联网

    xshell怎么连接vmware虚拟机_虚拟机centos7怎么联网XShell下载地址:https://en.softonic.com/download/xshell安装步骤默认即可,安装XShell完成后进行连接。文件——>新建配置连接信息在虚拟机中输入ifconfig即可获得主机IP信息,即inetaddr后面的数字。如果输出的主机IP是127.0.0.1,可以参考该篇博客解决这个问题。点击用户身份验证,输入用…

    2025年11月23日
    1
  • 21位数激活码生成[最新免费获取]「建议收藏」

    (21位数激活码生成)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年4月1日
    130
  • window下彻底卸载删除SQL Serever 2012

    window下彻底卸载删除SQL Serever 2012对于刚刚涉足数据库软件的小白们,肯定会遇到由于下载的数据库是英文版的,但是又由于自身的英文水平有限,一些专业的英文单词看的并不是很明白想着是否能够将英文的SQLserver2012汉化,但是由于有中文版的SQL,汉化这件事就很少有人弄了,所以在网上很少出现中文包大家肯定有过怎样才能将SQL彻底的卸载干净,毕竟安装目录加上实例目录加上就有10G,由于一些实例目录默认在系统C盘,占据了很大的一部

    2022年10月2日
    3

发表回复

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

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