如何在cout上使用isatty(),或者我可以假设cout ==文件描述符1? [英] how to use isatty() on cout, or can I assume that cout == file descriptor 1?

查看:93
本文介绍了如何在cout上使用isatty(),或者我可以假设cout ==文件描述符1?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个命令行实用程序,可以交互地或在脚本中使用管道或i / o重定向。我使用 cin cout 为i / o,我想写一个额外的EOL输出是控制台,这样用户提示将从下一行开始。在脚本中这将是有害的。



我可以假设 cin == 0,cout == 1 ?我理解,没有干净的方式来获取流的文件描述符。或者是?

解决方案

可以使用rdbuf()来更改std :: cin和std :: cout在你的程序内。如果你不这样做,可能相当安全地假设cin = 0,cout = 1和clog和cerr both = 2,因为C ++标准声明它们与C stdin,stdout和stderr同步,并且有POSIX启动时的文件描述符。


Well, the subject says it all, basically.

I have a command-line utility that may be used interactively or in scripts, using pipes or i/o redirection. I am using cin and cout for i/o, and I want to write an extra EOL at the end if the output is console, so that user prompt will start from the next line. Within scripts this would be harmful.

Can I assume cin == 0, cout == 1? I understand that there is no clean way to get the file descriptor of a stream. Or is it?

解决方案

It is possible to use rdbuf() to change the destination of std::cin and std::cout inside your program. If you don't do that, it is probably quite safe to assume that cin = 0, cout=1 and clog and cerr both = 2 as the C++ standard states that they are synchronized with C stdin, stdout and stderr and those have per POSIX those file descriptors at startup.

这篇关于如何在cout上使用isatty(),或者我可以假设cout ==文件描述符1?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆