写入终端,尽管通过 bash 中的管道重定向
writing to terminal despite redirection by a pipe in bash
我希望仍然能够以C++写入终端(以显示进度报告(,即使我的程序被用户使用 bash 中的管道重定向,例如使用命令:
myprogram | sort
有没有办法?
您不能也不应该尝试控制用户希望如何处理程序的输出。您应该努力以最好的意图使用标准流。
- 将信息性消息写入
std::cout
/stdout
。 - 将错误/警告消息写入
std::cerr
/stderr
。
如果用户希望看到程序的输出,同时仍然能够将输出保存到文件中,则可以使用 tee
。
program | tee filename
我找到了答案。
int fd = open(ctermid(NULL), O_WRONLY);
std::string text("hello my terminal!);
write(fd, text.c_str(), text.size());
close(fd);
即使标准,标准输出和标准被重定向,这也非常有效!
相关文章:
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- IPC使用多个管道和分支进程来运行Python程序
- 将bash参数传递给c++
- 如何创建函数管道,以便函数一个接一个地运行?
- Gstreamer 管道从命令 lne 到 c 代码
- 外壳包装器句柄/执行交互式命令管道C++ UNIX
- 将旧管道转换为现代 openGL 时出现问题
- 如何使用管道在父级和子级之间来回传递文件
- 在没有管理员权限的情况下连接到同一网络中的命名管道
- 如何测量管道延迟?
- 我如何使用此程序管道多个命令?C++
- 先进先出:一个进程永远不会从管道读取
- Node.js fs.open() 在尝试打开 4 个以上的命名管道 (FIFO) 后挂起
- 使用模板而不是虚拟方法的管道模式
- bash脚本在使用popen()时不返回代码以调用C ++程序
- 我可以写入关闭的套接字并强制纠正损坏的管道错误吗?
- 在 Azure DevOps 构建管道中使用英特尔C++编译器为 Linux 环境构建C++代码
- 如何在 bash 中使用管道在 c++ 中使用 getenv() 访问文件?
- 写入终端,尽管通过 bash 中的管道重定向
- 通过管道将输入从 Bash 传输到 C++ cin 中