在C / C ++中检查文件是否正在使用的方法? [英] Way to check in C/C++ if a file is in use?

查看:309
本文介绍了在C / C ++中检查文件是否正在使用的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以检查C / C ++中是否正在使用文件?还是我必须总是实施锁/信号灯以防止多个线程/进程同时访问任何文件?

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?

如果我们考虑使用Linux ,以及以下情况:我想使用预先定义的通信框架,通过RS-232通信将存储在设备A中的文件内容分块传输到另一设备B。发送此传输请求时,我想在发送 Ready to Transfer:OK响应之前,确认设备A中的任何进程都没有使用该文件,然后我将开始分块读取和传输数据。
是否可以在不对文件进行fopen / fclose的情况下检查文件是否已在使用中?

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?

推荐答案

实际上

fopen();

是发现此问题的最佳方法。

is the best way to find this out.

在接收端执行fopen(),如果成功,则发送 OK to receive消息。

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

这篇关于在C / C ++中检查文件是否正在使用的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆