473,385 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 13900
> 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 FileStream(@"C:\Data.txt", FileMode.Open,...
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 border. Can I make this happen with a single call to a...
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. This has worked fine for years…until yesterday. ...
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 browser. This all works with one small quirk: ...
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 is perfectly able to view the file. but my code...
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 probably created with Access 97, Western European...
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 spreadsheet as follows string filename =...
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 - 2003 document on the website, and the document gets...
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 able to launch MSAccess fine and even work with...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.