当前位置: 首页 > news >正文

Linux文件属性

1. Linux文件属性

  • 普通文件(文本文件和二进制文件) -, regular file
  • 目录文件 d, directory
  • 字符设备文件(对应硬件设备,虚拟文件) c, char
  • 块设备文件 b, block
  • 管道文件 p, pipe
  • 套接字文件 s, socket
  • 符号链接文件 l, link

2. Linux获取文件属性的API

Linux获取文件属性的API也是调用stat命令实现的。

  • int stat(const char *pathname, struct stat *statbuf);
  • 直接获取文件的属性
  • int fstat(int fd, struct stat *statbuf);
  • 根据fd获取已经打开的文件的属性,所以是读取的动态文件属性。
  • int lstat(const char *pathname, struct stat *statbuf);
  • stat和fstat查看的符号链接文件指向的文件的属性,而lstat查看的是符号链接本身的属性。

结构体:

       struct stat {
           dev_t     st_dev;         /* ID of device containing file */
           ino_t     st_ino;         /* Inode number */
           mode_t    st_mode;        /* File type and mode */
           nlink_t   st_nlink;       /* Number of hard links */
           uid_t     st_uid;         /* User ID of owner */
           gid_t     st_gid;         /* Group ID of owner */
           dev_t     st_rdev;        /* Device ID (if special file) */
           off_t     st_size;        /* Total size, in bytes */
           blksize_t st_blksize;     /* Block size for filesystem I/O */
           blkcnt_t  st_blocks;      /* Number of 512B blocks allocated */

           /* Since Linux 2.6, the kernel supports nanosecond
              precision for the following timestamp fields.
              For the details before Linux 2.6, see NOTES. */

           struct timespec st_atim;  /* Time of last access */
           struct timespec st_mtim;  /* Time of last modification */
           struct timespec st_ctim;  /* Time of last status change */

       #define st_atime st_atim.tv_sec      /* Backward compatibility */
       #define st_mtime st_mtim.tv_sec
       #define st_ctime st_ctim.tv_sec
       };
       
       st_mode是一个按位定义的文件类型标志,可以用
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#define FILE "a.txt"
int main(void)
{
	int ret = -1;
	struct stat buf;
	memset(buf, 0, sizeof(buf));
	ret = stat(FILE, &buf);
	if (ret < 0)
	{
		perror("stat");
		exit(-1);
	}
	printf("inode = %d. \n",buf.st_ino);

	int res = S_ISREG(buf.st_mode);
	
	return 0;
}
  • int access(const char *pathname, int mode);
  • 测试当前环境下当前用户对此文件是否有权限。
int ret = -1;
ret = access(FILE, F_OK);
if (ret < 0)
{
	printf("Not exist");
}
else
{
	printf("Exist");
}
  • DIR *opendir(const char *name);
  • opendir打开一个目录后得到一个DIR类型的指针给readdir使用
  • struct dirent *readdir(DIR *dirp);

readdir函数调用一次就会返回一个struct dirent类型的指针,这个指针指向一个结构体变量,这个结构体变量里面记录了一
个目录项(所谓目录项就是目录中的一个子文件)。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.dtcms.com/a/110456.html

相关文章:

  • hanzi-writer-miniprogram真机显示不出来Path2D问题已解决(真机能显示了!)
  • UE5Actor模块源码深度剖析:从核心架构到实践应用
  • webrtc 本地运行的详细操作步骤 1
  • LoRa模块通信距离优化:如何实现低功耗覆盖30公里无线传输要求
  • 基于lora的llama2二次预训练
  • 力扣算法ing(47 / 100)
  • 使用ssh连接上开发板
  • Java基础:面向对象进阶(二)
  • 创维E900V22C/E900V22D_S905L3(B)_安卓9.0_指示灯正常_线刷固件包
  • Oracle数据库数据编程SQL<4.2 锁机制>
  • YOLO与SSD对比
  • 前端Uniapp接入UviewPlus详细教程!!!
  • 从零到1搭建流媒体服务器
  • C++基础系列【35】巧用assert
  • ARM Cortex-M用于控制中断和异常处理的寄存器:BASEPRI、PRIMASK 和 FAULTMASK
  • 图形渲染: tinyrenderer 实现笔记(Lesson 5 - 7)
  • 【算法学习计划】回溯 -- 二叉树中的深搜
  • WebRTC技术简介及应用场景
  • 解决Spring参数解析异常:Name for argument of type XXX not specified
  • Linux命令-xargs
  • 25.4.3学习总结【Java】
  • Ubuntu 安装 VLC
  • Vue2(15) 自定义事件学习笔记
  • VTK知识学习(50)- 交互与Widget(一)
  • vue3+ts+element-plus 开发一个页面模块的详细过程
  • CExercise04_1位运算符_2 定义一个函数判断给定的正整数是否为2的幂
  • 通过第k个最大元素深入浅出快排和堆排序
  • 开箱即用的可视化AI应用编排工具 Langflow,可调用魔搭免费API作为tool
  • C++实现对象单例模式
  • SQL操作之:连接(JOIN)