将字符发送到控制台应用程序 [英] Send characters to console application

查看:36
本文介绍了将字符发送到控制台应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在终端窗口中运行的简单控制台应用程序读取和打印字符:

I have simple console application that runs in terminal window reads and prints character:

int main(int argc, char **argv, char **envp)
{
    while (true)
    {
        char c =getchar();
        printf("%c \n",c);

    }

}

现在我想做一个可以在第一个应用程序终端中模拟字符按下的测试应用程序.

Now I would like to make test application that could emulate character press in first application terminal.

我该走哪条路?为此,我应该使用哪些 API 函数?

Which way I should go? What API functions I should use for this purpose?

推荐答案

不需要特殊的 API 或其他任何东西.由于您的示例应用程序仅从标准输入中读取数据,因此您可以将内容发送到那里.

No need for special APIs or whatever. Since your sample application is only reading from standard input, you can just send stuff to there.

在终端中运行程序之前,使用 tty 命令检查其连接的终端.然后将数据发送到 tty 报告的 tty.

Before running the program in a terminal, check its connected terminal using tty command. Then send data to that tty that tty reports.

或者,获取正在运行的应用程序的 PID 并将数据发送到 /proc/$PID/fd/0,这样您就不需要检查 tty.

Alternatively, grab the PID of your running application and send data to /proc/$PID/fd/0 so you don't need to check for tty.

这篇关于将字符发送到控制台应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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