473,386 Members | 1,790 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,386 software developers and data experts.

how to tell if a file is open


Heya all,

How do I tell, prgrammatically, if a file is opened for read/write by
some other process? In unix .. of course.

My program needs to open files and work on it, and I want it to make
sure that the file is not being written to (e.g. by scp, cp, mv etc)
before I attampt to open it. Is there any function to see the status of
the file ? Using/implementing locking mechanism is out of the question.
If not provided by the language, is there anything provided by the Unix
operatins system ?

Shilpa

Sep 21 '06 #1
18 7646
sh************@gmail.com writes:
How do I tell, prgrammatically, if a file is opened for read/write by
some other process? In unix .. of course.
Unix questions are better asked in comp.unix.programmer.
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman
Sep 21 '06 #2
sh************@gmail.com wrote:
How do I tell, prgrammatically, if a file is opened for read/write by
some other process? In unix .. of course.
As Ben noted, this is best answered on comp.unix.programmer. However,
in C you can use the Shel Silverstein approach, similar to his
algorithm for determining whether a window is open:

#include <stdio.h>

int main(void)
{
FILE *fp;

if( (fp=fopen("foo.txt")) != NULL ) {
/* It wasn't open! Let's try another one! */
fclose( fp );
if( (fp=fopen("bar.txt")) != NULL ) {
/* Woohoo! */
fclose( fp );
}
else {
/* CRASH! Guess that one was... Let's try another one! */
/* ... */
}
}
return 0;
}

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Sep 21 '06 #3
BaG
I think this question qualifies to be put in here.

Ben Pfaff wrote:
sh************@gmail.com writes:
How do I tell, prgrammatically, if a file is opened for read/write by
some other process? In unix .. of course.

Unix questions are better asked in comp.unix.programmer.
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman
Sep 21 '06 #4

sh************@gmail.com wrote:
Heya all,

How do I tell, prgrammatically, if a file is opened for read/write by
some other process? In unix .. of course.

My program needs to open files and work on it, and I want it to make
sure that the file is not being written to (e.g. by scp, cp, mv etc)
before I attampt to open it. Is there any function to see the status of
the file ? Using/implementing locking mechanism is out of the question.
If not provided by the language, is there anything provided by the Unix
operatins system ?

Shilpa
Unix doesnt provide exclusive file locking. There are addon file lock
kludges but of course those would require every program to use the
kludges, and none do.

The easiest way is to not try reading the file directly, but instead
require that the providing program or batch file write the file
elsewhere, then when its done, rename the file into your special
directory. So if the file shows up, it's guarantreed to be whole.

Sep 21 '06 #5
If you want a standard C answer, which is all you can expect to
get comp.lang.c, then the answer is "You can't." Hope that
helps.

"BaG" <bi***********@gmail.comwrites:
I think this question qualifies to be put in here.

Ben Pfaff wrote:
>sh************@gmail.com writes:
How do I tell, prgrammatically, if a file is opened for read/write by
some other process? In unix .. of course.

Unix questions are better asked in comp.unix.programmer.
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman
--
"The way I see it, an intelligent person who disagrees with me is
probably the most important person I'll interact with on any given
day."
--Billy Chambless
Sep 21 '06 #6
Christopher Benson-Manica schrieb:
sh************@gmail.com wrote:
>>How do I tell, prgrammatically, if a file is opened for read/write by
some other process? In unix .. of course.

As Ben noted, this is best answered on comp.unix.programmer. However,
in C you can use the Shel Silverstein approach, similar to his
algorithm for determining whether a window is open:

#include <stdio.h>

int main(void)
{
FILE *fp;

if( (fp=fopen("foo.txt")) != NULL ) {
You forgot the "mode" argument...
/* It wasn't open! Let's try another one! */
fclose( fp );
if( (fp=fopen("bar.txt")) != NULL ) {
/* Woohoo! */
fclose( fp );
}
else {
/* CRASH! Guess that one was... Let's try another one! */
/* ... */
}
}
return 0;
}
I am not sure whether you mean that seriously.
fopen() returning NULL on an already opened file is not
guaranteed and neither is the converse.

Could you please clarify what you mean?
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 21 '06 #7
sh************@gmail.com wrote:
>
How do I tell, prgrammatically, if a file is opened for read/write
by some other process? In unix .. of course.
By asking in a newsgroup where unix is topical.
comp.unix.programmer comes to mind.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html

--
Posted via a free Usenet account from http://www.teranews.com

Sep 21 '06 #8
Christopher Benson-Manica wrote:
sh************@gmail.com wrote:
>How do I tell, prgrammatically, if a file is opened for read/write
by some other process? In unix .. of course.

As Ben noted, this is best answered on comp.unix.programmer.
However, in C you can use the Shel Silverstein approach, similar
to his algorithm for determining whether a window is open:

#include <stdio.h>

int main(void)
{
FILE *fp;

if( (fp=fopen("foo.txt")) != NULL ) {
/* It wasn't open! Let's try another one! */
fclose( fp );
if( (fp=fopen("bar.txt")) != NULL ) {
/* Woohoo! */
fclose( fp );
}
else {
/* CRASH! Guess that one was... Let's try another one! */
/* ... */
}
}
return 0;
}
Faulty. Please quote C & V where the C standard mentions anything
about a file being open when fopen is called.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html

--
Posted via a free Usenet account from http://www.teranews.com

Sep 21 '06 #9
BaG wrote:
I think this question qualifies to be put in here.

Ben Pfaff wrote:
sh************@gmail.com writes:
How do I tell, prgrammatically, if a file is opened for
read/write by some other process? In unix .. of course.
Unix questions are better asked in comp.unix.programmer.
--
"A lesson for us all: Even in trivia there are traps."
--Eric Sosman


--
Sep 21 '06 #10
BaG wrote:

Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or:
<http://www.caliburn.nl/topposting.html>

Text rearranged.

Ben Pfaff wrote:
sh************@gmail.com writes:
How do I tell, prgrammatically, if a file is opened for
read/write by some other process? In unix .. of course.
Unix questions are better asked in comp.unix.programmer.
I think this question qualifies to be put in here.

That's wrong. This group deals with ISO standard C. There is no way to
do that in ISO standard C. It's platform dependent, so a newsgroup
dedicated to the platform is needed. The problem specified UNIX, so a
the newsgroup Ben gave was the right one.


Brian
Sep 21 '06 #11
*** top-posting fixed ***
BaG wrote:
Ben Pfaff wrote:
>sh************@gmail.com writes:
>>How do I tell, prgrammatically, if a file is opened for
read/write by some other process? In unix .. of course.

Unix questions are better asked in comp.unix.programmer.

I think this question qualifies to be put in here.
No it doesn't. The moment the OP mentioned unix he made the whole
thing off-topic.

Kindly do not top-post. It is both rude and contrary to proper
behaviour on c.l.c. Read the following references.

--
Some informative links:
news:news.announce.newusers
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html

--
Posted via a free Usenet account from http://www.teranews.com

Sep 22 '06 #12
"Ancient_Hacker" <gr**@comcast.netwrites:
sh************@gmail.com wrote:
>How do I tell, prgrammatically, if a file is opened for read/write by
some other process? In unix .. of course.
[...]
>
Unix doesnt provide exclusive file locking.
[...]

That turns out not to be correct, making this an excellent example of
why we try not to answer off-topic questions here. It doesn't address
the question; the OP asked how to tell whether a file is opened by
another process, not how to implement a lock.

Try comp.unix.programmer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 22 '06 #13
Michael Mair <Mi**********@invalid.invalidwrote:

(W.R.T. the Shel Silverstein "file open" algorithm...)
You forgot the "mode" argument...
Argh!
I am not sure whether you mean that seriously.
Well, I was hoping it would be obvious that mentioning Shel
Silverstein implied <smiletags :-)

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Sep 22 '06 #14

Keith Thompson wrote:
"Ancient_Hacker" <gr**@comcast.netwrites:
sh************@gmail.com wrote:
How do I tell, prgrammatically, if a file is opened for read/write by
some other process? In unix .. of course.
[...]

Unix doesnt provide exclusive file locking.
[...]

That turns out not to be correct,...

Taken out of context, you are correct. But in the context of the guy's
full question, my answer *is* exactly correct. The system commands
and the standard C file library on Unix NEVER call the optional file
locking API's.

But you are correct inasmuch as file locking questions don't fully
match the ( somewhat narrow, de-facto) baliwick of this newsgroup.

Sep 22 '06 #15
Christopher Benson-Manica schrieb:
Michael Mair <Mi**********@invalid.invalidwrote:
>>I am not sure whether you mean that seriously.

Well, I was hoping it would be obvious that mentioning Shel
Silverstein implied <smiletags :-)
I read this name the first time in your message, and
http://en.wikipedia.org/wiki/Shel_Silverstein
did not really indicate what to think about this
reference... :-)
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 22 '06 #16
Michael Mair <Mi**********@invalid.invalidwrote:
I read this name the first time in your message, and
http://en.wikipedia.org/wiki/Shel_Silverstein
did not really indicate what to think about this
reference... :-)
Well, the Shel Silverstein algorithm for determining whether a window
is open is given in one of his poems, "Stone Telling":

How do we tell if a window is open?
Just throw a stone at it.
Does it make a noise?
It doesn't?
Well, it was open.
Now, let's try another ...
CRASH!
Hell, that one wasn't!
Let's try another!

Using this approach to determining whether files are open or not is
not recommended :-)

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Sep 22 '06 #17
"Ancient_Hacker" <gr**@comcast.netwrites:
Keith Thompson wrote:
>"Ancient_Hacker" <gr**@comcast.netwrites:
sh************@gmail.com wrote:
How do I tell, prgrammatically, if a file is opened for read/write by
some other process? In unix .. of course.
[...]
>
Unix doesnt provide exclusive file locking.
[...]

That turns out not to be correct,...

Taken out of context, you are correct. But in the context of the guy's
full question, my answer *is* exactly correct. The system commands
and the standard C file library on Unix NEVER call the optional file
locking API's.

But you are correct inasmuch as file locking questions don't fully
match the ( somewhat narrow, de-facto) baliwick of this newsgroup.
<OT>
My response was based on a very quick reading of the lockf(3) man
page. It appears that I misunderstood it, and I believe you're
correct.
</OT>

Ironically, this reinforces my point about off-topic answers. 8-)}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 22 '06 #18
Christopher Benson-Manica schrieb:
Michael Mair <Mi**********@invalid.invalidwrote:
>>I read this name the first time in your message, and
http://en.wikipedia.org/wiki/Shel_Silverstein
did not really indicate what to think about this
reference... :-)

Well, the Shel Silverstein algorithm for determining whether a window
is open is given in one of his poems, "Stone Telling":

How do we tell if a window is open?
Just throw a stone at it.
Does it make a noise?
It doesn't?
Well, it was open.
Now, let's try another ...
CRASH!
Hell, that one wasn't!
Let's try another!

Using this approach to determining whether files are open or not is
not recommended :-)
Ah, thank you :-)

-Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Sep 22 '06 #19

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

Similar topics

8
by: Peter Abel | last post by:
Hi all, I'm working under W2k with Python 2.2.2 (#37, Oct 14 2002, 17:02:34) on win32 I have a file *test_data.txt* with the following content: 0123456789 0123456789 abcdefghi...
3
by: Pernell Williams | last post by:
Hi all: I am new to Python, and this is my first post (and it won't be my last!), so HELLO EVERYONE!! I am attempting to use "xreadlines", an outer loop and an inner loop in conjunction with...
0
by: Prabhu Ramachandran | last post by:
Hi, I noticed peculiar behavior under Python-2.3.4 under Win32. When I run something like this: f = open('t.txt', 'wb') f.write('1\012'+'2\012'+'3\012') f.close() f = open('t.txt', 'r')...
2
by: Daniel Reber | last post by:
I have an application that is sending a file to a location on a network. I have a windows service that is monitoring that location for new files and then processing them. Sometimes the service...
8
by: pamelafluente | last post by:
Hi, I am invoking Firefox, and for testing IE, from my VB program. The instruction are of this kind: Process.Start("IExplore.exe", Parameter) Process.Start("Firefox.exe", Parameter) where...
1
by: Mike.Duffy | last post by:
Is there a trick you can use to know if someone has made your web page his "home"? At first, I thought to check if ( history.length == 0 ) or if ( document.referrer.href == ""). These both sort...
2
by: gdarian216 | last post by:
the program reads input from a file and then outputs the averages and grade. for some reason it is reading in the same line twice and it doesn't print out the grade. everything else is correct, if...
7
by: lawrence k | last post by:
I've got a music studio for a client. Their whole studio is run with Macintosh computers. Macintosh computers allow file names to have open white spaces, such as "animal hospital.mp3". I have a...
3
by: =?Utf-8?B?Um9nZWxpbw==?= | last post by:
hey, got a method here (that I took from somewhere) that gets a list of all the files in a folder., /// <summary> /// Method that gets all the files in a directory and returns an arraylist of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...

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.