当前位置: 技术问答>linux和unix
我用管道作为一个程序的标准输入时,程序报错说不是一个终端不能做为标准输入,请问怎么样管道成为它的标准输入呢???具体错误信息见内
来源: 互联网 发布时间:2015-06-16
本文导语: 我是先关闭程序的标准输入,然后用dup函数将管道作为一个程序的标准输入的,程序报错,错误信息为: Pseudo-terminal will not be allocated because stdin is not a terminal. 请问怎么样才能让管道作为这个程序的标准输入...
我是先关闭程序的标准输入,然后用dup函数将管道作为一个程序的标准输入的,程序报错,错误信息为:
Pseudo-terminal will not be allocated because stdin is not a terminal.
请问怎么样才能让管道作为这个程序的标准输入呢??
Pseudo-terminal will not be allocated because stdin is not a terminal.
请问怎么样才能让管道作为这个程序的标准输入呢??
|
The major use of duplicating a file descriptor is to implement redirection of input or output: that is, to change the file or pipe that a particular file descriptor corresponds to.
Here is an example showing how to use dup2 to do redirection. Typically, redirection of the standard streams (like stdin) is done by a shell or shell-like program before calling one of the exec functions to execute a new program in a child process.
So, to redirect standard input to a file, the shell could do something like:
pid = fork ();
if (pid == 0)
{
char *filename;
char *program;
int file;
...
file = TEMP_FAILURE_RETRY (open (filename, O_RDONLY));
dup2 (file, STDIN_FILENO);
TEMP_FAILURE_RETRY (close (file));
execv (program, NULL);
}
Here is an example showing how to use dup2 to do redirection. Typically, redirection of the standard streams (like stdin) is done by a shell or shell-like program before calling one of the exec functions to execute a new program in a child process.
So, to redirect standard input to a file, the shell could do something like:
pid = fork ();
if (pid == 0)
{
char *filename;
char *program;
int file;
...
file = TEMP_FAILURE_RETRY (open (filename, O_RDONLY));
dup2 (file, STDIN_FILENO);
TEMP_FAILURE_RETRY (close (file));
execv (program, NULL);
}
|
程序贴出来看一下。
|
贴代码。
|
dup调用是复制一个描述符,你已经将stdin关闭了,还复制个屁?