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

Posted on 2003-12-07
Last Modified: 2010-04-15
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.

Question by:gangisetti
LVL 16

Expert Comment

ID: 9895109
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.


Author Comment

ID: 9895119
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..
LVL 45

Expert Comment

ID: 9895677
>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)
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.


Expert Comment

ID: 9895842
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....!!
LVL 16

Expert Comment

ID: 9895877

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.

Author Comment

ID: 9895888
Thanks! PaulCaswell and others as well.
LVL 45

Accepted Solution

Kent Olsen earned 50 total points
ID: 9901324
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.)


Expert Comment

ID: 9941513
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.


Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question