472,342 Members | 1,525 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

Opening an already Opened file

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"

Nov 25 '05 #1
7 13600
> 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.

--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
System Design with Sequence Diagrams in PDF and Word EMF

Nov 25 '05 #2

EventHelix.com wrote:
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.


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"
--
EventStudio System Designer 2.5 - http://www.EventHelix.com/EventStudio
System Design with Sequence Diagrams in PDF and Word EMF


Nov 25 '05 #3
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.

Nov 25 '05 #4
In message <11**********************@f14g2000cwb.googlegroups .com>,
emanshu <mu*******@gmail.com> writes

EventHelix.com wrote:
> 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.


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.

--
Richard Herring
Nov 25 '05 #5

"emanshu" <mu*******@gmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
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
Nov 25 '05 #6

"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:7e**************@baesystems.com...
In message <11**********************@f14g2000cwb.googlegroups .com>,
emanshu <mu*******@gmail.com> writes

EventHelix.com wrote:
> 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.
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 :-)


A file opened for read normally doesn't have a 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..

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.

--
Richard Herring

Nov 27 '05 #7
In message <0Q*****************@fe06.lga>, Jim Langston
<ta*******@rocketmail.com> writes

"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:7e**************@baesystems.com...
In message <11**********************@f14g2000cwb.googlegroups .com>,
emanshu <mu*******@gmail.com> writes

EventHelix.com wrote:
> 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.

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 :-)


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.

--
Richard Herring
Nov 28 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: PM | last post by:
Has anyone found a way to open a file exclusively where it will fail if the file is already open, i have tried the following _FileStream = new...
14
by: D. Alvarado | last post by:
Hello, I am trying to open a window containing an image and I would like the image to be flush against the window -- i.e. have no padding or...
1
by: Bill Engel | last post by:
I have a Microsoft Access 2000 database that resides on our server. We have five users who gain access to this database through a network drive. ...
5
by: Brad | last post by:
In several aspx applications I export crytal reports to pdf, xls and doc files and then the aspx page writes the selected export file to the client...
4
by: Omko H | last post by:
I am trying to open %windir%\pfirewall.log. and I just can't get it to work (I am a absolute newbie to VB.net) the strange thing is that notepad...
0
by: asklucas | last post by:
Hi there, I got an MS Access DB, which is causing problems when the client PC is running using a Traditional Chinese codepage. The DB was...
0
by: f.oualid | last post by:
Hi, Would anyone know how I could open an already opened excel workbook. I have Windows Form with a button, and when clicked it opens the...
1
by: captainwin | last post by:
Here's the problem: - Website is ASP.NET 1.0 - Server is Windows Server 2003 - Browser is IE 6/7 - Client requests a mail merged MS Word 97 -...
0
by: bbrewder | last post by:
I am struggling with some MSAccess automation issues. Basically, we have a .Net application that uses MSAccess for reporting (legacy code). We are...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.