Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 413
  • 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.

Thanks!
Gangisetti
0
gangisetti
Asked:
gangisetti
1 Solution
 
PaulCaswellCommented:
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
 
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..
0
 
sunnycoderCommented:
>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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
ssnkumarCommented:
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
 
PaulCaswellCommented:
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
 
gangisettiAuthor Commented:
Thanks! PaulCaswell and others as well.
0
 
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.)

Kent
0
 
SanketDesaiCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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