Linux
Linux判断文件描述符是否有效
Linux判断文件描述符是否有效
Linux判断文件描述符是否有效[cpp] viewplaincopy/** * author:kangear@163.com * date :2015-01-17 * func :check if the fileDescriptor is fine. */ #include #incl
Linux判断文件描述符是否有效
- /**
- * author:kangear@163.com
- * date :2015-01-17
- * func :check if the fileDescriptor is fine.
- */
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <errno.h>
- int main() {
- int fd = -1;
- fd = open("/tmp/isatty.c", O_RDONLY);
- // close(fd);
- if(fcntl(fd, F_GETFL))
- printf("%m\n");
- close(fd);
- }
- /**
- * version : 1.1
- * date : 2015-02-05
- * func : check if the fileDescriptor is fine.
- */
- #include <unistd.h>
- #include <fcntl.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <stdio.h>
- struct stat _stat;
- /**
- * On success, zero is returned. On error, -1 is returned, and errno is set
- * appropriately.
- */
- int check_fd_fine(int fd) {
- struct stat _stat;
- int ret = -1;
- if(!fcntl(fd, F_GETFL)) {
- if(!fstat(fd, &_stat)) {
- if(_stat.st_nlink >= 1)
- ret = 0;
- else
- printf("File was deleted!\n");
- }
- }
- if(errno != 0)
- perror("check_fd_fine");
- return ret;
- }
- int main() {
- int fd = -1;
- fd = open("/dev/ttyUSB1", O_RDONLY);
- if(fd < 0) {
- perror("open file fail");
- return -1;
- }
- // close(fd);
- sleep(5);
- if(!check_fd_fine(fd)) {
- printf("fd okay!\n");
- } else {
- printf("fd bad!\n");
- }
- close(fd);
- return 0;
- }
更多推荐
- · 网卡速率和双工模式的配置
- · Linux虚拟文件系统之文件系统卸载(sys_umount())
- · Linux系统下超级终端Minicom的使用方法(例如:连接交换机,路由器等)转http://baike.baidu.com/view/2911642.htm?fr=ala0_1
- 3933
- 0
- 0
- 0
扫一扫分享内容
- 分享
已为社区贡献2条内容
回到
顶部
顶部
所有评论(0)