曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 3703|回复: 0
打印 上一主题 下一主题

[文件I/O] isatty() -- 判断一个文件描述符是否指向一个终端

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34397
跳转到指定楼层
楼主
发表于 2012-1-2 11:03:18 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
isatty() 原型如下:
[C++] 纯文本查看 复制代码
#include <unistd.h>
int isatty(int fd);

该函数判断一个打开了的文件描述符是否指向一个终端。

测试代码
[C++] 纯文本查看 复制代码
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>


void isatty_printer(const int fd)
{


    if (isatty(fd)) 
        printf ("Yes, it is a tty\n");
    else
        printf ("No, it is not a tty\n");
}
int main()
{
    int fd = 100;
    
    isatty_printer(fd);


    fd = STDIN_FILENO;


    isatty_printer(fd);


    fd = open("/dev/tty", O_RDONLY);
    
    isatty_printer(fd);


    return 0;
}

运行输出:
./isatty
No, it is not a tty
Yes, it is a tty
Yes, it is a tty
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2025-6-18 04:55 , Processed in 0.082544 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表