how to tell if a file is open

S

shilpa.saraogi

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
 
B

Ben Pfaff

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.
 
C

Christopher Benson-Manica

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;
}
 
B

Ben Pfaff

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 said:
I think this question qualifies to be put in here.
 
A

Ancient_Hacker

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.
 
M

Michael Mair

Christopher said:
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
 
C

CBFalconer

Christopher said:
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:
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
 
D

Default User

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.

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
 
C

CBFalconer

*** top-posting fixed ***
BaG said:
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:
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
 
K

Keith Thompson

Ancient_Hacker said:
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.
 
C

Christopher Benson-Manica

(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 <smile> tags :)
 
A

Ancient_Hacker

Keith said:
Ancient_Hacker said:
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.
 
C

Christopher Benson-Manica

Michael Mair said:
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 :)
 
K

Keith Thompson

Ancient_Hacker said:
Keith said:
Ancient_Hacker said:
(e-mail address removed) 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. :cool:}
 
M

Michael Mair

Christopher said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top