c 函数笔记

作用

	判断文件描述符是否为终端(输出设备句柄)

相关联的函数 ttyname

作用

	返回终端名(路径)

使用

	ttyname(int)

相关联函数 fileno

作用

把文件流指针转换成文件描述符

使用

	fileno(FILE *)

返回值

!= null 终端名(路径)
== null 文件描述符不是终端

头文件

	#include <sdtio.h>

返回值

	1 文件描述符为终端
	0 文件描述符不是终端

示例

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
void fd_test(int fd){
    if(isatty(fd)){
        printf("fd is a tty tty path = %s\n",ttyname(fd));

    }else{
        printf("fd is not a tty\n");
    }
}
void test (char *path)
{
    int fd = open(path,O_RDONLY);
    if(fd < 0){
        printf("open fail\n");
        return;
    }
    fd_test(fd);
    close(fd);
}
void test1 (char * path){
    FILE *file = fopen(path,"r");
    if(!file){
        printf("fopen fail");
        return;
    }
    fd_test(fileno(file));
    fclose(file);

}
int main ( int argc, char *argv[] )
{
    test("/dev/tty");
    test("./test");
    fd_test(fileno(stdin));
    fd_test(fileno(stdout));
    fd_test(fileno(stderr));
    test1("/dev/tty");
    test1("./test");

    return 0;
}

结果

3 is a tty tty path = /dev/tty
fd is not a tty
0 is a tty tty path = /dev/pts/0
1 is a tty tty path = /dev/pts/0
2 is a tty tty path = /dev/pts/0
3 is a tty tty path = /dev/tty
fd is not a tty
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值