【发布时间】:2013-06-21 15:52:39
【问题描述】:
我有以下代码 它打印到屏幕上:哈哈 到文件:
haha
hello
Father finished
如果我删除第 6 行和第 7 行,我会得到不同的结果 为什么?
int main()
{
// creates a new file having full read/write permissions
int fd = open("myfile", O_RDWR|O_CREAT, 0666);
write(fd, "haha\n", 5);
close(fd); // line 6
fd = open("myfile", O_RDWR); // line 7
close(0);
close(1);
dup(fd);
dup(fd);
if (fork() == 0)
{
char s[100];
dup(fd);
scanf("%s", s);
printf("hello\n");
write(2, s, strlen(s));
return 0;
}
wait(NULL);
printf("Father finished\n");
close(fd);
return 0;
}
【问题讨论】:
-
删除第 6 行和第 7 行会得到什么不同的结果?
-
文件中没有任何内容:哈哈
标签: c file-descriptor dup