I'm using Linux for a while and today I found an interesting thing.
Saying that I have an infinite program, which keeps writing strings into a file.
If I delete the file while running the program, I thought some error would generate by the program. To my surprise, nothing happened...
Here is how I create the ininite program with c++11:
int main()
{
std::ofstream outputFile("./target");
int i = 0;
while (true) {
outputFile << i << endl;
i++;
std::this_thread::sleep_for(chrono::milliseconds(1000));
}
return 0;
}
So after running it, I get a new file name target
. If I delete this file (rm ./target
), no error will be generated.
My questions are:
Answer to 1 & 2: When you believe you have deleted the file, what you actually have done is that you have deleted the "link" to the file. Since your program is already running, the file is actually open. The file is still there and can be written to and read from if you have access to it. So, no error message since you are still writing to the file.