我有一个问题,为什么 write() 系统调用在我从 shell 中删除目标文件后仍然可以成功写入内容?
当我在前几次成功写入后删除文件时,我期待看到 write() 失败,但它没有,我不知道它写入哪里。
谁能给我解释一下,谢谢!!
这是我的测试示例代码:
int fd = open ("./test.txt", O_CREAT | O_RDWR | O_APPEND, 0666);
if (fd == -1)
error_and_exit ("open() failed");
ssize_t written = 0;
char buf[] = "apple\n";
while (true)
{
printf ("perform write...\n");
written = write (fd, buf, sizeof(buf));
if (written <= 0)
error_and_exit ("write failed");
printf ("=> wrote %d bytes\n", written);
printf ("sleep %d...\n\n", SLEEP_TIME);
sleep (SLEEP_TIME);
}