Reputation: 17553
How does C++ behave if it is reading a file (say a csv file with 20,000 lines) and halfway through the read, another process deletes the file (lets say it is an ifstream).
Do I get a seg fault, or does the vector I am storing the lines in just have 10,000 lines and no error to indicate that something went wrong?
Similarly, what happens if I am writing via ofstream and it gets interrupted by a deletion? In that case, if the interrupt is halfway through, does my output file only contain the last 10,000 lines?
Upvotes: 3
Views: 1421
Reputation: 129491
It depends on an OS.
If you are on a Unix-y OS, you will continue reading/writing the data since deletion is merely an update to a containing directory and/or inode, so you don't affect the file's data.
HOWEVER, if the disk blocks where the file data resided may be re-used (again depends on the OS) by a newer file WHILE you are reading, you will basically read wrong file's data once you finish with the data in the buffer.
If you are on Windows, as Seth Carnegie correctly noted in a comment, the file automatically gets locked (and thus undeletable) if it is opened for reading.
Upvotes: 4
Reputation: 688
It is highly system dependent I guess. As Linux may continue working while Windows give you some headache.
Upvotes: -1