我有以下代碼 它打印到屏幕上:哈哈 到文件:打開描述符並關閉,爲什麼這很重要?
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行時會得到什麼不同的結果? –
沒什麼好看的 該文件包含:haha –