I opened a file stream to a very big file using fopen. Before performing any read operation on that stream, I deleted the file using unlink(). And still, I was able to read the whole file.
I am guessing that there is a buffer associated with the stream, which holds the data of the file. But obviously that buffer will have a limit. That was the reason why I chose a_big_file whose size was 551126688 bytes or 526MB.
I want to know what is the exact reason behind it. Here is the test code that I used.
#include <stdio.h>
#include <unistd.h>
int main(){
FILE *fp;
long long int file_size = 0;
int bytes_read = 0;
char buf[1];
fp = fopen("a_big_file", "r");
unlink("a_big_file");
while(0 != (bytes_read = fread(buf, 1, 1, fp))){
file_size += bytes_read;
}
printf("file_size is %llu\n", file_size);
return 0;
}
Output: file_size is 551126688