Detecting if a file is open

  • Follow


{ Looks more like an OS question, but let's see if we can elicit
  some C++ discussion out of it. -mod }

Hi Everyone,

I'm trying to detect if an excel file is open (not if it exists but is
actually opened by the user). One approach that worked is before
generating the file, I check if it exists and if it does, I try
deleting it. If it allows for the file to be deleted, then it is not
open.

The problem with this approach is that the file is deleted (in the
event that it is not open). I also tried creating an ofstream handle
and associated it with the excel file. Unfortunately, this association
automatically causes the file to be opened. So the is_open() call
always returns true even if the file has not been opened by the user.

Does anyone know of a more elegant of detecting if a file is open?

Thank you for your time,
Puja

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply ptmalhotra (1) 5/14/2009 1:48:02 AM

On 14 mai, 10:48, Puja <ptmalho...@gmail.com> wrote:

> Does anyone know of a more elegant of detecting if a file is open?

When the application launches, it creates a file. When it terminates,
it deletes that file.
If the file exists, the application runs.

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Mathias 5/14/2009 11:48:21 AM


Puja wrote:
> { Looks more like an OS question, but let's see if we can elicit
>   some C++ discussion out of it. -mod }
> 
> Hi Everyone,
> 
> I'm trying to detect if an excel file is open (not if it exists but is
> actually opened by the user). One approach that worked is before
> generating the file, I check if it exists and if it does, I try
> deleting it. If it allows for the file to be deleted, then it is not
> open.
> 
> The problem with this approach is that the file is deleted (in the
> event that it is not open). I also tried creating an ofstream handle
> and associated it with the excel file. Unfortunately, this association
> automatically causes the file to be opened. So the is_open() call
> always returns true even if the file has not been opened by the user.
> 
> Does anyone know of a more elegant of detecting if a file is open?
> 

As far I'm aware neither the iostream lib nor fopen and friends provides 
for the notion that a file on disk is opened by another process/user.

The best thing you can do IMHO is to use the Win32 file functions to 
determine the state of the file.

On the other hand, what is the problem you are trying to solve? Do you 
really need to know if it's open, or just need to know if you should 
write to it?

br,
Martin

-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]

0
Reply Martin 5/14/2009 12:13:29 PM

2 Replies
207 Views

(page loaded in 0.075 seconds)

Similiar Articles:













7/23/2012 10:35:23 PM


Reply: