如何判断文件是否打开 [英] how to tell if a file is open

查看:133
本文介绍了如何判断文件是否打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Heya all,


如何通过编程方式判断是否已打开文件进行读/写$ / b $ b其他一些过程?在unix ..当然。


我的程序需要打开文件并对其进行处理,我希望它能使

确保文件不是被写入(例如通过scp,cp,mv等)

,然后我才开始打开它。是否有任何功能可以查看

文件的状态?使用/实现锁定机制是不可能的。

如果语言没有提供,那么Unix

操作系统是否提供了什么?


Shilpa

解决方案

sh ************ @ gmail.com 写道:


我如何告诉我,如果一个文件被读取/写入由/ b
其他一些进程?在unix ..当然。



在comp.unix.programmer中更好地询问Unix问题。

-

课程对我们所有人来说:即使在琐事中也有陷阱。

--Eric Sosman


sh ************ @ gmail.com 写道:


如何以文件方式判断文件是否被打开以进行读/写
其他一些过程?在unix ..当然。



正如Ben所说,这在comp.unix.programmer上得到了最好的解答。但是,在C中你可以使用Shel Silverstein方法获得
,类似于他的

算法来确定窗口是否打开:


#include< stdio.h>


int main(无效)

{

FILE * fp;


if((fp = fopen(" foo.txt"))!= NULL){

/ *它没有打开!让我们试试另一个吧! * /

fclose(fp);

if((fp = fopen(" bar.txt"))!= NULL){

/ *哇哦! * /

fclose(fp);

}

else {

/ * CRASH!猜猜一个是......让我们试试另一个! * /

/ * ... * /

}

}

返回0;

}


-

C. Benson Manica |我*应该*知道我在说什么 - 如果我

cbmanica(at)gmail.com |不,我需要知道。火焰欢迎。


我认为这个问题有资格放在这里。


Ben Pfaff写道:
< blockquote class =post_quotes> sh************@gmail.com 写道:


如何通过编程方式判断是否打开了一个文件进行读/写
其他一些过程?在unix ..当然。



在comp.unix.programmer中更好地询问Unix问题。

-

课程对我们所有人来说:即使在琐事中也有陷阱。

--Eric Sosman



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

解决方案

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


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.


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


这篇关于如何判断文件是否打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆