【发布时间】:2016-01-26 05:51:10
【问题描述】:
第一轮
通过一些例子,我知道如何将 stdio 重定向到 null。
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd = open("/dev/null", O_RDWR);
int in, out, err;
if (fd < 0) {
perror("open file error!");
return -1;
}
printf("test1\n");
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
printf("test2\n");
close(fd);
return 0;
}
执行代码后,我的控制台显示:
test1
test2 被重定向到 /dev/null。
但是,现在,我想将 stdio 从 /dev/null 回滚到标准输入和输出。
我该怎么做?
第二轮
感谢您的回复。
实际上,我遇到了一个程序重定向stdio(例如示例1)并fork我的程序(例如示例2)的问题。
示例 1#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd = open("/dev/null", O_RDWR);
if (fd < 0) {
perror("open file error!");
return -1;
}
printf("test1\n");
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
execl("hello_world", NULL);
close(fd);
return 0;
}
示例 2
#include <stdio.h>
int main()
{
printf("Hello World!\n");
return 0;
}
运行示例 1 后,我的控制台显示:
test1
如何更改示例 2 以将 printf() 重定向到控制台? 谢谢。
【问题讨论】:
-
我不确定在每种情况下都可以做到这一点。例如,一旦文本被传送到 Windows 控制台,除非您手动清除屏幕,否则它会一直存在。