• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 417
  • Last Modified:

How to check whether a file is already in use or not ?

Hi all,

I need to write a routine, which will check whether a given file is already in use. If the file is already opened for writing, I need to get that status.

I need to do this in windows environment, I think this doesnt matter much

I tried doing this using,

fopen, OpenFile.. but in all these functions, there is an extra operation of getting a file handle and then closing it. Can we determine this without actually getting a file handle  and then closing it?  I want to remove this overhead.

1 Solution
Use the 'access' function. It allows you to check access of a file to several levels, including read/write as you want. It is most useful for checking the existence of a file but it should achieve your requirements.

gangisettiAuthor Commented:
Thanks! paulcaswell.

But, I even tried with that. Access function is returning whether it is allowed to open a file in a particular mode or not. It is not checking whether the file is in use or not. It is just returning, if the file has write permissions on it or not..
>I need to do this in windows environment, I think this doesnt matter much
I guess it does ...

I can think of a way to do it on linux but I am not too familiar with windows ...

I do not believe standard C provides any function for doing what you want (unless open count is included in st_nlink field of struct stat (filled by call to stat() function)
The Firewall Audit Checklist

Preparing for a firewall audit today is almost impossible.
AlgoSec, together with some of the largest global organizations and auditors, has created a checklist to follow when preparing for your firewall audit. Simplify risk mitigation while staying compliant all of the time!

Unix maintains a data structure (File Table) which keeps count of the references to each file in memory.
Some such mechanism must be there in windows too....!!

You are right. 'access' only tells you about potential access to the file. If you check for read+write access this generally means that no-one has the file open but does not guarantee this. Beyond that, if you really need to know, you will probably have to code for the O/S more carfefully.

Under the 'DOS' based ones (W95, W98, ME), you can probably walk the system FCB (File Control Blocks) to see if the file you are interested in is in the list. I've never done this but I understand it is possible so some careful searches should find some sample source.

Sadly, this will probably not work under NT based ones (NT, 2K, XP) as FCBs dont, as such, exist.

Seriously, I would question your need for this information before embarking on a trip as tangled as this.

Perhaps someone else has a better idea. Maybe reposting you question under the Networks group would get some answers. There may be some network services that give you this information.
gangisettiAuthor Commented:
Thanks! PaulCaswell and others as well.
Kent OlsenData Warehouse Architect / DBACommented:
Hi gangisetti,

According to both Micro$oft and Borland, Windows operating systems have no easy way to get the information that you desire.  The Borland C++ Builder runtime libraries support this, but to use it your application will have to be compiled and linked in the C++ Builder environment.  (I like Borland's compiler, IDE, and libraries so this something that I would do if it were my application.)

Interestingly, DOS 3.x allowed you to place such restrictions on file accesses, but as C has evolved I guess that they "fixed" that.  :(

If you're wanting "exclusive use" of the file, there are several things that you can do.

Use the lock() API in io.h to set a lock over the entire file.  Other applications will be prevented from reading or writing anything under the lock() until you release it.

Create a "lock file".  When your application wants use of the file:

open ("MyFileName.lck", O_CREAT|O_EXCL|O_TRUNC);

If the open fails, the lock already exists and the application should not open the file.  If the open succeeds, no other program that adheres to your locking protocol has the file and you can use it freely.  Once you close the file you must delete the lock file.

Of course, if your question is *nix related, all of this changes.  (Probably for the better.)

Hi gangisetti,

         To get your required result, you need to have access to File system table (atleast for windows platform). File system table will certainly have status of the file, from that you will be able to know whether the file, you want to access, is already opened or not.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Firewall Management 201 with Professor Wool

In this whiteboard video, Professor Wool highlights the challenges, benefits and trade-offs of utilizing zero-touch automation for security policy change management. Watch and Learn!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now