开发者

File in use C/C++

开发者 https://www.devze.com 2023-03-09 09:51 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消