检查文件是否存在 [英] Checking if a file exists

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

问题描述

这是检查文件已存在的好方法吗?

#include< stdio.h>

#include< stdlib。 h>

int ask(const char * prompt);

typedef char filename [FILENAME_MAX];


int main(int argc,char * argv [])

{

FILE * in,* out;

filename in_name,out_name;

int overwrite = 0;

/ * get filenames * /

out = fopen(out_name," r")

if (out!= NULL){

fclose(out);

printf(文件%s已存在。,out_name);

if(问(你要覆盖它吗?)== 1)

覆盖++;

其他

退出(EXIT_FAILURE);

}

/ *等... * /

}


-

#include< stdio.h>

#include< stdlib.h>

int main(void)/ * Don不要在家里试试* / {

const size_t dim = 256; int i;

for(i = 0; malloc(dim); i ++)/ * nothing * /;

printf("你已经完成了!%zu \ n",i * dim);

puts(" \ n\\\
- Army1987");返回0;

}

Is this a good way to check wheter a file already exists?

#include <stdio.h>
#include <stdlib.h>
int ask(const char *prompt);
typedef char filename[FILENAME_MAX];

int main(int argc, char *argv[])
{
FILE *in, *out;
filename in_name, out_name;
int overwrite = 0;
/* get filenames */
out = fopen(out_name, "r")
if (out != NULL) {
fclose(out);
printf("File %s already exists. ", out_name);
if (ask("Do you want to overwrite it?") == 1)
overwrite++;
else
exit(EXIT_FAILURE);
}
/* etc... */
}

--
#include <stdio.h>
#include <stdlib.h>
int main(void) /* Don''t try this at home */ {
const size_t dim = 256; int i;
for (i=0; malloc(dim); i++) /*nothing*/ ;
printf("You''re done! %zu\n", i*dim);
puts("\n\n--Army1987"); return 0;
}

推荐答案

Army1987说:
Army1987 said:

这是检查文件已存在的好方法吗?


#include< stdio.h>

#include< stdlib.h> ;

int ask(const char * prompt);

typedef char filename [FILENAME_MAX];


int main(int argc ,char * argv [])

{

FILE * in,* out;

filename in_name,out_name;

int overwrite = 0;

/ * get filenames * /

out = fopen(out_name," r")

if( out!= NULL){

fclose(out);

printf(文件%s已存在。,out_name);

if(问(你要覆盖它吗?)== 1)

覆盖++;

其他

退出( EXIT_FAILURE);

}

/ * etc ... * /

}
Is this a good way to check wheter a file already exists?

#include <stdio.h>
#include <stdlib.h>
int ask(const char *prompt);
typedef char filename[FILENAME_MAX];

int main(int argc, char *argv[])
{
FILE *in, *out;
filename in_name, out_name;
int overwrite = 0;
/* get filenames */
out = fopen(out_name, "r")
if (out != NULL) {
fclose(out);
printf("File %s already exists. ", out_name);
if (ask("Do you want to overwrite it?") == 1)
overwrite++;
else
exit(EXIT_FAILURE);
}
/* etc... */
}



它与你将获得ISO C一样好,但它不是

可靠。 open可能由于其他原因而失败,而不是文件的不存在(例如磁盘错误,没有读取权限等)。并且没有

保证文件将在fclose之后继续存在,或者

它将继续不存在(如果确实它不存在)

fopen失败。


将此功能抽象到您的非便携式东西中。模块,以及

请参考您的实现文档,了解如何解决这个问题(如果确实有一个解决方案)这个问题(如果确实有一个解决方案)那么它

目标。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

It''s about as good as you''re going to get under ISO C, but it''s not
reliable. The open might fail for other reasons than the non-existence
of the file (e.g. disk error, no read permission, etc). And there is no
guarantee that the file will continue to exist after the fclose, or
that it will continue not to exist (if indeed it did not exist) after
the fopen failure.

Abstract this functionality into your "non-portable stuff" module, and
consult your implementation''s documentation for details of how to solve
this problem (if indeed there is a solution) on the platform that it
targets.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


Army1987< pl ******** @ for.itwrote:
Army1987 <pl********@for.itwrote:

这是检查文件已存在的好方法吗?
Is this a good way to check wheter a file already exists?


out = fopen(out_name," r")

if(out!= NULL){

fclose(out);

printf(文件%s已存在。,out_name);

if(ask(" Do you)想要覆盖吗?)== 1)

overwrite ++;
out = fopen(out_name, "r")
if (out != NULL) {
fclose(out);
printf("File %s already exists. ", out_name);
if (ask("Do you want to overwrite it?") == 1)
overwrite++;



不,这不是个好主意。可能有很多其他原因

为什么fopen()失败,从失去许可开始

它用于读取例如用完了文件描述符。所以

你不应该盲目地认为文件不存在

只是因为读取模式下的fopen()失败了(顺便说一下,它是

也可以在多任务系统上创建文件

在你进行测试和时间之间的另一个过程

你打开它)。如果你想避免覆盖

文件中的数据(如果它已经存在),请在追加模式中将其打开并检查是否带有ftell()的
如果你在文件的开头(但是,

仍然不允许区分文件的情况,而不是现有的
和现有的文件但是为空)。


问候,Jens

-

\ Jens Thoms Toerring ___ jt@toerring.de

\ __________________________ http:// toerring。 de


大约在2007年4月7日上午6:51,Army1987声明如下:
At about the time of 4/7/2007 6:51 AM, Army1987 stated the following:

这是检查文件已存在的好方法吗?


#include< stdio.h>

#include< ; stdlib.h>

int ask(const char * prompt);

typedef char filename [FILENAME_MAX];


int main(int argc,char * argv [])

{

FILE * in,* out;

filename in_name,out_name;

int overwrite = 0;

/ * get filenames * /

out = fopen(out_name," ; r")

if(out!= NULL){

fclose(out);

printf(" File%s已经存在。 ",out_name);

if(问(你要覆盖它吗?)== 1)

覆盖++;

else

退出(EXIT_FAILURE);

}

/ *等... * /

}
Is this a good way to check wheter a file already exists?

#include <stdio.h>
#include <stdlib.h>
int ask(const char *prompt);
typedef char filename[FILENAME_MAX];

int main(int argc, char *argv[])
{
FILE *in, *out;
filename in_name, out_name;
int overwrite = 0;
/* get filenames */
out = fopen(out_name, "r")
if (out != NULL) {
fclose(out);
printf("File %s already exists. ", out_name);
if (ask("Do you want to overwrite it?") == 1)
overwrite++;
else
exit(EXIT_FAILURE);
}
/* etc... */
}



我认为Microsoft实现了一个文件存在调用。在Unix系统上,

你可以stat(2)文件并检查errno = ENOENT。当然,这是

不便携,但它确实有效。

-

Daniel Rudy


电子邮件地址已经过base64编码以减少垃圾邮件

解码电子邮件地址使用b64decode或uudecode -m


为什么极客喜欢电脑:看聊天日期触摸grep make unzip

带状视图手指支架fcsk更多fcsk是喷雾umount睡眠

I think that Microsoft implements a file exists call. On Unix systems,
you can stat(2) the file and check if errno = ENOENT. Granted, this is
not portable, but it works.
--
Daniel Rudy

Email address has been base64 encoded to reduce spam
Decode email address using b64decode or uudecode -m

Why geeks like computers: look chat date touch grep make unzip
strip view finger mount fcsk more fcsk yes spray umount sleep


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

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