简体   繁体   English

从已删除的文件中读取

[英]read from a file which is deleted

I wrote a small program in C where I opened a file successfuly, then called sleep for 20 sec. 我用C编写了一个小程序,在其中成功打开了一个文件,然后调用sleep保持20秒钟。 In that 20 sec I deleted the open file using rm from shell. 在那20秒内,我使用rm从shell删除了打开的文件。 After sleep the program reads the data successfully and prints it on screen. 睡眠后,程序成功读取数据并将其打印在屏幕上。

int bytes_read;
FILE *fp = fopen("/tmp/file", "r");
sleep(20);
bytes_read = fread(buf, 1, 5, fp);
buf[bytes_read] = '\0';
printf("%s", buf);

I expected it to read 0 bytes, but it prints the actual data in the file. 我希望它读取0字节,但它将在文件中打印实际数据。 What is the explanation behind this behaviour. 这种行为背后的解释是什么。

In linux and other POSIX systems you don't delete files. 在Linux和其他POSIX系统中,您不会删除文件。 You just remove an inode from a directory. 您只需从目录中删除一个索引节点。 As long as there is a file descriptor open on a file it will not be deleted. 只要在文件上打开了文件描述符,它就不会被删除。 Only when the last link to the inode and the last open file descriptor went away. 仅当指向索引节点的最后一个链接和最后打开的文件描述符消失时。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检测从已删除的mmap文件读取? - how to detect read from deleted mmap file? 我想从包含整数和浮点数的文件中读取数据 - I want to read data from a file which contains integers and floats 如何从C中的文件中保存我在数组中读取的数据 - How to save the data which i read in an array from a file in C 如何从整数之前的文件中读取字符串 - How can i read a string from a file which is before a integer c - 如何将从文本文件读取的字符重写为c中的新文本文件? - How can i rewrite the characters which read from text file to a new text file in c? 我想从显示哪个字符出现得最多或最少出现的文本文件中读取(键盘或文本文件)? :) - I want to read from text file that displays which character appears most or least often (keyboard or text file) ? :) 我需要从一个我们不知道矩阵尺寸的文件中读取一个矩阵 - I need to read a matrix from a file which we dont know the matrix dimensions 如何从 C 中包含非英语(土耳其语)内容的文件中读取十进制数字 - How to read decimal numbers from a file which has non-English (Turkish) content in C 如何打印从文件中读取的包含特殊字符的字符串? 如何不带特殊字符打印? - How to print a string, which includes special characters and was read from a file? How to print it without special characters? 如何将每一行放入从 c 程序中的文件读取的数组中? - How do I put each line in an array which read from the file in c program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM