检查文件是否存在的好方法? [英] Good way of checking if file exists ?

查看:81
本文介绍了检查文件是否存在的好方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在使用

if(open(" file",O_EXCL)!= -1){printf(" File does

exists")} else { printf(文件不存在); }


a检查文件是否存在的好方法,如果不存在我应该怎么做?

is using
if(open("file",O_EXCL) != -1){ printf("File does
exists")}else{printf("file does not exists"); }

a good way of checking if a file exists or not, if not how should I do it ?

推荐答案

标准的C兼容方式可以是:


FILE * fp = fopen(" file"," r");

if(fp){

//存在

fclose(fp);

}否则{

//不存在

}


Geiregat Jonas写道:
a standard C compatible way could be:

FILE *fp = fopen("file","r");
if( fp ) {
// exists
fclose(fp);
} else {
// doesnt exist
}


Geiregat Jonas wrote:
正在使用
if(open (file,O_EXCL)!= -1){printf(文件确实存在)} else {printf(文件不存在); }

检查文件是否存在的好方法,如果不存在我应该怎么做?
is using
if(open("file",O_EXCL) != -1){ printf("File does
exists")}else{printf("file does not exists"); }

a good way of checking if a file exists or not, if not how should I do it ?



Robert Frunzke <滚装************** @ freenet.de>潦草地写道:
Robert Frunzke <Ro**************@freenet.de> scribbled the following:
标准的C兼容方式可能是:
FILE * fp = fopen(" file"," r");
if(fp){
//存在
fclose(fp);
}否则{
//不存在


不存在,存在但是不可读,存在但已经太多

许多锁或其他类似的原因。
}
a standard C compatible way could be: FILE *fp = fopen("file","r");
if( fp ) {
// exists
fclose(fp);
} else {
// doesnt exist
Doesn''t exist, exists but isn''t readable, exists but already has too
many locks, or other similar reasons.
}




在纯ISO标准C中确定存在是不可能的。


-

/ - Joona Palaste(pa ** ***@cc.helsinki.fi)-------------芬兰-------- \

\-- http://www.helsinki.fi/~palaste --------- ------------规则! -------- /

人类中的孤雌生殖将导致建立一个新的宗教信仰。

- John Nordberg



It''s impossible to check existence for certain in pure ISO standard C.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Parthenogenetic procreation in humans will result in the founding of a new
religion."
- John Nordberg


Geiregat Jonas< en *** @ sdf-eu.org>写道:
Geiregat Jonas <en***@sdf-eu.org> wrote:
正在使用
if(open(" file",O_EXCL)!= -1){printf(" File does
exists) } else {printf(" file not exists"); }

检查文件是否存在的好方法,如果不存在我应该怎么做?
is using
if(open("file",O_EXCL) != -1){ printf("File does
exists")}else{printf("file does not exists"); }

a good way of checking if a file exists or not, if not how should I do it ?




我建议使用' 'fopen''而不是使用标准的c函数。


但是,标准C并没有提供任何保证的方法来确定

文件是否存在或不。即使文件确实存在,fopen也会失败。


但是,它可能对你的系统来说是合理的方法,

但这个问题只能由知识渊博的人回答你的特定系统。


-

== Eric Gorr ====== === http://www.ericgorr.net ======== = ICQ:9293199 ===

因此,智能的考虑总是包括

的利益和伤害。 - 孙子

==侮辱,就像暴力一样,是无能的最后避难所...... ===



I would recommend using ''fopen'' instead to use a standard c function.

However, standard C doesn''t provide any guaranteed way to determine
whether a file exists or not. fopen can fail even if a file does exist.

However, it is possible that it is a reasonable method for your system,
but that question could only be answered by people knowledgeable with
your specific system.

--
== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both
benefit and harm." - Sun Tzu
== Insults, like violence, are the last refuge of the incompetent... ===


这篇关于检查文件是否存在的好方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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