473,657 Members | 2,609 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13969
> 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************ **********@f14g 2000cwb.googleg roups.com>,
emanshu <mu*******@gmai l.com> writes

EventHelix.c om 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*******@gmai l.com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.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******** ******@baesyste ms.com...
In message <11************ **********@f14g 2000cwb.googleg roups.com>,
emanshu <mu*******@gmai l.com> writes

EventHelix.co m 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*******@rock etmail.com> writes

"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:7e******* *******@baesyst ems.com...
In message <11************ **********@f14g 2000cwb.googleg roups.com>,
emanshu <mu*******@gmai l.com> writes

EventHelix.c om 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
714
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 FileStream(@"C:\Data.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.None); which does not prevent me from opening the file even if another user has the file open ie Notepad, it will prevent me from saving the file in notepad until i close the file in the app. I can then save changes made in...
14
11073
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 border. Can I make this happen with a single call to a window.open function? I would prefer not to create a separate HTML page. So far all I have is the basic var cwin = window.open('images/KJV-THANKS.gif', 'Thanks', 'width=243,height=420,'); cwin.focus();
1
1944
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. This has worked fine for years…until yesterday. Now this database opens, closes and performs every duty in between painfully slowly. This database runs smoothly if opened on the server upon which it resides. Further, I copied it to one of my local workstation drives and it worked fine from...
5
3316
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 browser. This all works with one small quirk: When I run any of the web apps on my workstations iis website, the pdf download always produces the "File Download" dialog, even though I have Confirm after Download turned off. When I select to Open the file from this dialog the file opens in...
4
3747
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 is perfectly able to view the file. but my code keeps failing. amongst other things i tried: Dim s2 As New System.IO.FileStream(FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)
0
1821
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 probably created with Access 97, Western European codepage. However, the DB I got is now already in Access 2002 / 2003 file format, thus should be already converted to Unicode. When I open the DB on my PC with Western European codepage, it runs
0
2787
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 spreadsheet as follows string filename = @"C:\Documents\Test.xls";
1
2173
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 - 2003 document on the website, and the document gets created on the server. - Need to send the document to the client browser and delete the file from the server. Previously, the web application already did this, but after some hardware and location changes, the application no longer performs this...
0
2018
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 able to launch MSAccess fine and even work with the MSAccess COM objects to run our reports (using GetObject(<MDB Path>) - see http://support.microsoft.com/default.aspx?scid=kb;en-us;317113&Product=vbNET for info on how to do this). The problem that we are running into is that if our Access...
0
8397
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8310
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7333
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1957
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.