file stat

stat是linux下获取文件状态的一个命令,同时也可以做为函数来获取文件状态。

 int stat(const char *restrict path, struct stat *restrict buf);


int file_stat()
{
	struct stat stat1;
	int ret = -1;
	int file_count = 0;
	DIR *dir1 = NULL;
	struct dirent * dirent1 = NULL;
	memset(&stat1, 0, sizeof(stat1));
	ret = stat(FILENAME, &stat1); //获取stat结构体状态
	if(ret < 0)
	{
		perror("stat");
		return -1;
	}
	if ((stat1.st_mode & S_IFMT) == S_IFREG) //判断是否为文件
	{
	   /* Handle regular file */
		printf("%s is a file.\n", FILENAME);
	}
	if(S_ISREG(stat1.st_mode))        //判断是否为文件的另一种写法
	{
		printf("%s is a file.\n", FILENAME);
	}

	printf("file size=%lu, ino=%lu, uid=%u, mode=%d\n", stat1.st_size, stat1.st_ino, stat1.st_uid, stat1.st_mode);
	
	if(access(FILENAME, F_OK) == 0)  //判断文件是否存在
	{
		printf("F_OK\n");
	}
	if(access(FILENAME, W_OK) == 0)  //是否有写权限
	{
		printf("W_OK\n");
	}

	if(chmod(FILENAME, S_IWOTH) == 0)   //赋予other 写权限,同时其他组的权限也清空了
	{
		printf("chmod S_IWOTH success.\n");
	}

	dir1 = opendir("src");  //打开一个目录
	if(dir1 == NULL)
	{
		perror("opendir");
		return -1;
	}
	dirent1 = readdir(dir1);
	while(dirent1 != NULL)   //遍历出所有普通文件
	{
		if (dirent1->d_type == DT_REG) //DT_REG 判断是否为普通文件.
		{
			file_count++;
			printf("This is a regular file,file name =%s\n", dirent1->d_name); //DT_DIR      This is a directory.
		}
		dirent1 = readdir(dir1);
	}
	printf("file_count=%d\n", file_count);
	return 0;
}
stat结构体

           struct stat {
               dev_t     st_dev;         /* ID of device containing file */
               ino_t     st_ino;         /* inode number */
               mode_t    st_mode;        /* protection */
               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;     /* blocksize 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
           };


translate in english: 在`ls /dev -l`命令输出中,c 文件、b 文件和 p 文件(管道文件)有以下区别: ### 类型定义 - **c 文件(字符设备文件)**:一般在`/dev`目录下,用于表示字符设备。字符设备以字符流的形式进行数据传输,每次处理一个字符,如键盘、鼠标等设备通常对应字符设备文件 。 - **b 文件(块设备文件)**:同样一般位于`/dev`目录,代表块设备。块设备以数据块为单位进行数据传输,数据块大小通常为 512 字节或 4096 字节等,像硬盘、U盘 等存储设备对应的就是块设备文件 。 - **p 文件(管道文件)**:用于进程间通信。管道文件可以实现两个或多个进程之间的数据传递,一个进程向管道写入数据,另一个进程从管道读取数据 。 ### 命令输出标识 在`ls -l`命令的输出中,第一列的第一个字符用于标识文件类型,c 表示字符设备文件,b 表示块设备文件,p 表示管道文件 。 ### 示例代码 以下是一个简单的 C 语言示例,用于模拟`ls -l`命令输出并识别文件类型: ```c #include <stdio.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> int main() { struct stat fileStat; if (stat("/dev/tty", &fileStat) < 0) { perror("stat"); return 1; } if (S_ISCHR(fileStat.st_mode)) { printf("/dev/tty is a character device file.\n"); } else if (S_ISBLK(fileStat.st_mode)) { printf("/dev/tty is a block device file.\n"); } else if (S_ISFIFO(fileStat.st_mode)) { printf("/dev/tty is a pipe file.\n"); } return 0; } ```
最新发布
11-04
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值