通过fstat函数判断描述符类型
来源:互联网 发布:淘宝做什么产品比较好 编辑:程序博客网 时间:2024/05/20 05:08
fstat函数用于返回关于文件的信息到一个struct stat结构中,stat结构中的st_mode可以用来区分文件类型。
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 file system I/O */ blkcnt_t st_blocks; /* number of 512B blocks allocated */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last status change */ };
一些用来判断st_mode的宏
S_ISREG(m) is it a regular file?S_ISDIR(m) directory?S_ISCHR(m) character device?S_ISBLK(m) block device?S_ISFIFO(m) FIFO (named pipe)?S_ISLNK(m) symbolic link? (Not in POSIX.1-1996.)S_ISSOCK(m) socket? (Not in POSIX.1-1996.)
示例:
#include <stdio.h>#include <sys/types.h>#include <sys/stat.h>#include <unistd.h>#include <sys/socket.h>#include <fcntl.h>#include <stdlib.h>main(){struct stat st;int sockfd, fd;sockfd = socket(AF_INET, SOCK_STREAM, 0);if (sockfd == -1){perror("socket error");exit(-1);}fd = open("/home/liyuchen/dev/net.h", O_RDWR);if (fd == -1){perror("open error");exit(-1);}fstat(sockfd, &st);if (S_ISREG(st.st_mode))printf("文件描述符\n");if (S_ISSOCK(st.st_mode))printf("套接字描述符\n");fstat(fd, &st);if (S_ISREG(st.st_mode))printf("文件描述符\n");if (S_ISSOCK(st.st_mode))printf("套接字描述符\n");}
0 0
- 通过fstat函数判断描述符类型
- fstat函数
- fstat函数
- fstat函数
- fstat()函数
- fstat函数
- fstat函数
- C语言fstat()函数:由文件描述词取得文件状态
- stat/fstat/lstat函数
- stat, fstat, lstat 函数
- stat、fstat、lstat函数
- stat和fstat函数
- fstat函数使用
- Linux函数fstat说明
- stat/fstat/lstat函数
- 通过文件名取类型图标和描述
- 通过UA判断浏览器类型
- 通过JS判断浏览器类型
- hdu 4008 树形dp
- sqlite3 相关
- Zoj 2112 Dynamic Rankings
- HDU 2076 夹角有多大(题目已修改,注意读题)
- POJ-1308 & HDOJ-1325 Is It A Tree? 解题报告
- 通过fstat函数判断描述符类型
- 基于TCP的项目学习历程(一)实现简单的socket请求。瞎扯的,不要评论。
- /proc/sys/kernel/printk
- 【P000-009】分析模块——四大类
- ACM steps quicksum
- 写一网络爬虫有感
- Jquery(Ajax)高级UI设计
- HDU 1026 Ignatius and the Princess I
- 数论合集 I