#include
#include
#include
#include
#include
int main(void) ...{
int filedes;
if((filedes=open("dd.txt",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))<0)
fprintf(stderr,"%s ","open file error");
close(1);
dup(filedes);
close(filedes);
execlp("ls","-l",(char * )0);
return 0;
}
上面是输出的重定向,听说输入的重定向只要把cosle(1),改为close(0)就行,但程序却提示“ 读取失败: -: 错误的文件描述符
”
代码如下:
#include
#include
#include
#include
#include
#include
int main(void) ...{
int filedes;
if((filedes=open("dd.txt",O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR))<0)
fprintf(stderr,"%s ","open file error");
close(0);
dup(filedes);
close(filedes);
execlp("sort","sort",(char * )0);
return 0;
}