to find whether the file is already open

yogish

Registered
What is the function in c to find out whether the file is already opened by some application other than lsof?
 
"C" ? This is not Windows, it is OS X. Plus have you ever did a command button+i ("Get Info" key combination) on a file to see what process the file is to open in? Lastly to see what is running can be done via the GUI way by open /Applications/Utilities/Activity Monitor. The /Applications/Utilities/Terminal command would be: top - then hit return button. To quit the Terminal application Top just hold control key+z.
 
I think he might have been referring to the C programming language, not so much a "C:" drive. Maybe some sort of script that would run in csh.
 
There is no way to do it in C other than to call 'fopen' with the exclusive flag and test the return code to see if it failed or not.
 
You could also use the command-line tool lsof to list open files. Pipe the results into grep with the file path you want to check, like so:

lsof | grep "/path/to/file"

You can also call external commands from C. I forget the exact function call to do it in "raw" C (been using Python and Cocoa for so long...).

Note that you might want to do "sudo lsof" instead, since lsof run as an ordinary user will only return files open by that user, not system processes.

I use this technique in Terminal quite often when disks fail to eject because "a file is in use". "sudo lsof | grep /Volumes/Drive_Name" usually tells me what program I need to quit.
 
Back
Top