写入新打开的终端窗口

时间:2015-12-21 11:55:46

标签: 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包含所需信息时,控制台窗口中没有写入任何内容。如何写入控制台终端窗口?

1 个答案:

答案 0 :(得分:0)

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

/dev/pts/25

这是coppyes从foo文件到bar和控制台的一切程序:

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

您的程序只会打开与pts/2不同的设备pts/25,您说它是您的控制台。

相关问题