Way to check in C/C++ if a file is in use?

元气小坏坏 提交于 2019-12-12 01:43:33

问题


Is there any way to check if a file is in use in C/C++? Or do I have to ALWAYS implement a lock/semaphore to prevent simultaneous access of any file by multiple threads/processes?

If we consider Linux, and the following scenario: I want to transfer,in chunks, the contents of a file stored in device A to another device B through RS-232 communication, using a pre-defined communication framework. When the request for this transfer comes, I want to verify the file is NOT being used by any process in device A, before sending a "Ready to Transfer : OK" response, following which I will start reading and transmitting the data in chunks. Is there a way to check file if is already in use without doing fopen/fclose on the said file?


回答1:


actually

fopen();

is the best way to find this out.

Do fopen() on the receiving end, if it is successful, send the "OK to receive" message.



来源:https://stackoverflow.com/questions/17732743/way-to-check-in-c-c-if-a-file-is-in-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!