由于在 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/