//具体不懂得地方你晚上再问我吧
#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<fcntl.h>
#include<string.h>
int main(int argc, char *argv) {
int fd[2];
int len;
pid_t pid;
char filename[10];
char childbuf[10];
if (pipe(fd) < 0) {
perror("pipe error!");
exit(1);
}
if ((pid = fork()) < 0) {
perror("fork error!");
exit(1);
}
if (pid == 0) {
close(fd[1]);
len = read(fd[0], childbuf, 100);
childbuf[len] = '\0';
printf("%s\n", childbuf);
if (execlp("cat", "cat", childbuf, (char*) 0) < 0) {
perror("exec error!");
exit(1);
}
} else {
close(fd[0]);
printf("请输入文件名\n");
scanf("%s", filename);
write(fd[1], filename, strlen(filename));
waitpid(pid, NULL, 0);
return 0;
}
}
温馨提示:答案为网友推荐,仅供参考