c - 写入新打开的终端窗口

标签 c linux terminal tty

由于在 Linux 中一切都是文件,我想打印到终端窗口中打开的控制台。

我在 Linux 中打开了控制台并编写了命令 tty。在输出中我有:

/dev/pts/25

这是将所有内容从 foo 文件复制到 bar 和控制台的程序:

/* Trivial file copy program using low-level I/O */

#include <fcntl.h>
#include <stdlib.h>
#define BSIZE 16384

void main()
{
  int fin, fout,con; /* Input and output handles */
  char buf[BSIZE];
  int count;

  if ((con  = open("/dev/pts/2", O_WRONLY)) < 0) {
    perror("open con ");
    exit(1);
  }

  if ((fin  = open("foo", O_RDONLY)) < 0) {
    perror("foo");
    exit(1);
  }

  if ((fout = open("bar", O_WRONLY | O_CREAT, 0644)) < 0) {
    perror("bar");
    exit(2);
  }

  while ((count = read(fin, buf, BSIZE)) > 0)
  {
    write(fout, buf, count);
    write(con, buf, count);
  }

  close(fin);
  close(fout);
  close(con);
}

不幸的是,控制台窗口中没有任何内容,而 bar 包含所需的信息。如何写入控制台终端窗口?

最佳答案

我在 Linux 中打开了控制台并编写了命令 tty。在输出中我有:

/dev/pts/25

这个程序将 foo 文件中的所有内容复制到 bar 和控制台:

…
  if ((con  = open("/dev/pts/2", O_WRONLY)) < 0) {
…

你的程序只打开一个设备 pts/2 不同于 pts/25 你说的是你的控制台。

关于c - 写入新打开的终端窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34395126/

相关文章:

c - 为微型改进 Sprintf

macos - 命令行工具已安装,使用 "Software Update"安装更新?

c++ - Unix/C++ : Open new terminal and redirect output to it

javascript - 使用nodejs更改系统/操作系统时间

linux - 为什么 glob lstat 匹配条目?

php - 从在linux终端中运行的php脚本停止无限循环

c++ - 访问 dlopen 标志

C管道多条消息只收到一条

c - 无法从 C 中的 HashTable 中删除值

linux - Xvfb服务器不能在不同用户下工作