File in use C/C++
I'm learning C/C++ right now and I am reading about file operations. Suppose a program A is working with an external file (say, a text file) and another another program B is, say, trying to move the file (or worse, delete it). Is it possible to tell the OS to inform the program B that the file is in use, ev开发者_StackOverflow社区en though it was not created by program A?
What you're trying to do is called file locking. Search for "file locking in C".
A file is a resource.
If you happen to open
one in C/C++, or any other language for that matter, the OS "lends" this file to your program. While you have control of a file (resource) the OS prevents other processes from taking control over it (i.e. moving the file, deleting the file, etc.).
This is why it's important to close
a file after you're done working with it. This tells the OS that you no longer control this resource and other processes can fully access them.
精彩评论