如何判断文件是否打开

如何判断文件是否打开

本文介绍了如何判断文件是否打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! Heya all, 如何通过编程方式判断是否已打开文件进行读/写$ / b $ b其他一些过程?在unix ..当然。 我的程序需要打开文件并对其进行处理,我希望它能使 确保文件不是被写入(例如通过scp,cp,mv等) ,然后我才开始打开它。是否有任何功能可以查看 文件的状态?使用/实现锁定机制是不可能的。 如果语言没有提供,那么Unix 操作系统是否提供了什么? Shilpa 解决方案 在comp.unix.programmer中更好地询问Unix问题。 - 课程对我们所有人来说:即使在琐事中也有陷阱。 --Eric Sosman 正如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 |不,我需要知道。火焰欢迎。 在comp.unix.programmer中更好地询问Unix问题。 - 课程对我们所有人来说:即使在琐事中也有陷阱。 --Eric SosmanHeya all,How do I tell, prgrammatically, if a file is opened for read/write bysome other process? In unix .. of course.My program needs to open files and work on it, and I want it to makesure 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 ofthe file ? Using/implementing locking mechanism is out of the question.If not provided by the language, is there anything provided by the Unixoperatins system ?Shilpa 解决方案Unix questions are better asked in comp.unix.programmer.--"A lesson for us all: Even in trivia there are traps."--Eric SosmanAs Ben noted, this is best answered on comp.unix.programmer. However,in C you can use the Shel Silverstein approach, similar to hisalgorithm 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 Icbmanica(at)gmail.com | don''t, I need to know. Flames welcome.Unix questions are better asked in comp.unix.programmer.--"A lesson for us all: Even in trivia there are traps."--Eric Sosman 这篇关于如何判断文件是否打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-29 09:56