Opening an already Opened file

E

emanshu

HI all,

I an designing an application in C++. i want to open file requested by
end user but i want to reflect an error to user if file is already
opened by some other application..
will anybody tell me that how to know that the file is already open or
closed..

any help is highly appreciated...

thanks,
Munish Nayyar
emanshu "IM"
 
E

EventHelix.com

I an designing an application in C++. i want to open file requested by
end user but i want to reflect an error to user if file is already
opened by some other application..
will anybody tell me that how to know that the file is already open or
closed..

fopen will return NULL if the file is already locked by another
application.
 
E

emanshu

EventHelix.com said:
fopen will return NULL if the file is already locked by another
application.

but that logic fails if some other application has opened an file and
has no lock on it.
please share if you have any idea so that in my own application i can
implement the algorithm of not opening an file if it is already opened
by other application..

thanks,
Munish Nayyar
emanshu "IM"
 
D

Dervish

There is no way to do this. Programs must communicate through some sort
of locking (provided by OS or home brew).
The problem is that application can follow following logic:
1) Open file. Read file to memory. Close file.
2) Change it in memory (this can take hours).
3) Open file. Blindly write to disk. Close file.
In this case file is "opened"/edited by two apps, but this fact is not
visible even for OS.

However if file is opened and not closed there possible can be a chance
to discover that file is opened by another app. OS must track the list
of opened files, so read some MANs or MSDNs and you possible find out
how to find list of opened files in your OS. But please note, that this
does not solve your problem. If another app opens critical file a
second after your app checked that file is not opened and foreign app
does not check locking - it will still work in parallel with your
application. So, I repeat, there is no way to protect file without some
locking mechanism, used by all apps in a system.
 
R

Richard Herring

but that logic fails if some other application has opened an file and
has no lock on it.

(A nice trick if you can do it :)
please share if you have any idea so that in my own application i can
implement the algorithm of not opening an file if it is already opened
by other application..
Standard C++ has no concept of "other application". It's up to the
operating system to enforce things like file locking. If there is a way
to do what you want, it will be OS-specific, so you should probably be
asking in a group devoted to the relevant OS.
 
M

Mike Wahler

emanshu said:
HI all,

I an designing an application in C++. i want to open file requested by
end user but i want to reflect an error to user if file is already
opened by some other application..
will anybody tell me that how to know that the file is already open or
closed..

The only thing the C++ library mechanisms for opening a file
can tell you is whether the attempt succeeded or failed. If
it failed, reason(s) for *why* it failed are necessarily platform
specific. Most implementations provide extensions for things like this
when they apply.

Note that it's conceivable for an OS to have a file system where it's
perfectly valid to open an already open file.

This platform neutrality of C++ is also why it's not possible
(using only standard code) to determine conclusively whether
a file exists or not.

-Mike
 
R

Richard Herring

Jim Langston said:
A file opened for read normally doesn't have a lock on it.

I'd have thought it was more likely to have a lock which permits
multiple opening for reading, but prevents writing. But all this is
platform-specific and off topic here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top