By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,784 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

how to tell if a file is open

P: n/a

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
Share this Question
Share on Google+
18 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
*** 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

P: n/a
"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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.