Solved

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

Posted on 2003-12-07
10
362 Views
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.

Thanks!
Gangisetti
0
Comment
Question by:gangisetti
10 Comments
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 9895109
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
0
 
LVL 1

Author Comment

by:gangisetti
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..
0
 
LVL 45

Expert Comment

by:sunnycoder
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)
0
 
LVL 8

Expert Comment

by:ssnkumar
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....!!
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 16

Expert Comment

by:PaulCaswell
ID: 9895877
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.
0
 
LVL 1

Author Comment

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

Accepted Solution

by:
Kdo 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.)

Kent
0
 
LVL 1

Expert Comment

by:SanketDesai
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.

Sanket.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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 is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now