通过linux终端将数据发送到另一个进程的stdin [英] Sending data to stdin of another process through linux terminal
问题描述
我一直试图将数据发送到正在运行的进程的stdin.这是我的工作:
I've been trying to send data to stdin of a running process. Here is what I do:
-
在终端中,我启动了一个c ++程序,该程序仅读取并打印一个字符串.代码摘录:
In a terminal I've started a c++ program that simply reads a string and prints it. Code excerpt:
while (true) {
cin >> s;
cout << "I've just read " << s << endl;
}
我得到正在运行的程序的PID
I get the PID of the running program
结果: text
出现在运行程序的终端中.请注意,不是我刚刚阅读文本
,而是简单的 text
.我在做什么错,该怎么做才能打印我刚读过文字"
?
Result: text
appears in the terminal where the program is run. Note, not I've just read text
, but simply text
.
What am I doing wrong and what should I do to get this thing to print 'I've just read text'
?
推荐答案
在启动C ++程序时,需要确保其输入来自管道而不是来自终端.您可以使用 cat |myapp
来做到这一点.运行后,您可以将应用程序的PID用于 echo text>/proc/PID/fd/0
When you're starting your C++ program you need to make sure its input comes from a pipe but not from a terminal. You may use cat | myapp
to do that. Once it's running you may use PID of your application for echo text > /proc/PID/fd/0
这篇关于通过linux终端将数据发送到另一个进程的stdin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!