Link to home
Start Free TrialLog in
Avatar of gangisetti
gangisetti

asked on

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.

Thanks!
Gangisetti
Avatar of PaulCaswell
PaulCaswell
Flag of United Kingdom of Great Britain and Northern Ireland image

Gangisetti,
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.

Paul
Avatar of gangisetti
gangisetti

ASKER

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..
Avatar of sunnycoder
>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)
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....!!
Gangisetti,

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.
Thanks! PaulCaswell and others as well.
ASKER CERTIFIED SOLUTION
Avatar of Kent Olsen
Kent Olsen
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

Sanket.