Reputation: 288
How to check if file is already opened using boost if file is not opened then removed that file other wise do nothing
boost::filesystem::wpath file("c://test.txt");
if(boost::filesystem::exists(file))
{
if(here i want a check that file is already open or not, if open then run else)
{
boost::filesystem::remove(file);
}
else
{
}
}
Upvotes: 3
Views: 1703
Reputation: 393124
It's the job of the OS to prevent/allow this.
Each OS has it's own ways of locking for exclusive use, in which case deletion will fail anyways.
Other OSes (POSIX) will unlink the file entry from the inode, and the file keeps remaining accessible to the processes that have the file opened. When the last use of the inode goes away, the file is actually deleted.
In short, don't try to detect up front, just see whether deletion failed. Otherwise you'll run into the race condition mentioned
How are you going to deal with situations, where the file is opened in between your check and your attempt to remove it? here
It seems you have missed the point of that (seeing your reply), and Mike explained
Upvotes: 1