文件存在 [英] File exist
问题描述
大家好
有谁能告诉我如何查看文件是否存在。我的意思是什么时候
你使用这个命令
FILE * fp;
if(!fp)
//无法打开文件
不显示为什么它不能打开它,可能是文件不存在。现在
告诉我该怎么做!
谢谢
Hi all
Can anyone tell me how can I check that a file exist or no.I mean when
you use this commands
FILE *fp;
if(!fp)
//Could not open the file
doen''t show why it can not open it,may be the file doesn''t exist.Now
tell me what should I do!
Thanks
推荐答案
pa****@gmail.com 认为:
pa****@gmail.com opined:
大家好
任何人都可以告诉我如何检查文件是否存在。我的意思是
当你使用这个命令时
FILE * fp;
if(!fp)
//无法打开文件
不显示为什么无法打开它,可能是文件不存在。现在告诉你我该怎么办!
您可以尝试阅读`errno`(来自< errno.h>)确实得到错误代码,
,前提是你的实现给你一个,并且它是明智的(标准要求它不需要
)。在调用`fopen()`之前,你必须将`errno`设置为
0,因为库函数永远不会这样做。你需要参考C编译器附带的文档来查看
可用的'errno`代码,以及它们是否可以帮助你。
>
如果您的实现确实设置了有用的错误代码,那么您可以使用
来自< string.h>的strerror()`获取一个可读的错误字符串,或者来自< stdio.h>的
`perror()`。在你的手册中查找它们。
再次,如果你的实现没有提供合理的错误
代码/消息,你就会感冒。 OTH,依靠你的
实现错误代码/消息更有可能使你的代码不可移动
- 曾经听说过.cshrc?
Hi all
Can anyone tell me how can I check that a file exist or no.I mean
when you use this commands
FILE *fp;
if(!fp)
//Could not open the file
doen''t show why it can not open it,may be the file doesn''t exist.Now
tell me what should I do!
You can try reading `errno` (from <errno.h>) do get to the error code,
provided your implementation gives you one, and it''s sensible (it''s
not required by the Standard to do that). You''d have to set `errno` to
0 before calling `fopen()`, as library functions never do that. You
should consult documentation that came with your C compiler to see
what `errno` codes are available, and whether they can help you.
If your implementation does set useful error codes, you can then use
`strerror()` from <string.h> to get a huan-readable error string, or
`perror()` from <stdio.h>. Look them up in your manual.
Again, if your implementation does not provide sensible error
codes/messages, you''re out in the cold. OTH, relying on your
implementation error codes/messages is more likely than not to make
your code non-portable.
--Ever heard of .cshrc?
这是波斯尼亚的一个城市。对吗?
(在comp.os.linux.misc中讨论命令的直观性。)
< http://clc-wiki.net /wiki/Introduction_to_comp.lang.c>
That''s a city in Bosnia. Right?
(Discussion in comp.os.linux.misc on the intuitiveness of commands.)
<http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
我认为这就是你要找的东西:
FILE * fp
main(){
fp = fopen(" something.txt"," r"); //打开阅读
如果(!fp)printf(文件不存在);
else prinf(" file exists");
fclose(fp);
}
I think this is what you are looking for :
FILE *fp
main(){
fp=fopen("something.txt","r"); // open for reading
if (!fp) printf ("file doesn''t exist");
else prinf("file exist");
fclose(fp);
}
MH写道:
MH wrote:
我认为这就是你要找的:
文件* fp
main(){
FP =的fopen(QUOT; something.txt"," R"); //打开阅读
if(!fp)printf(文件不存在);
else prinf(文件存在);
fclose(fp);
}
I think this is what you are looking for :
FILE *fp
main(){
fp=fopen("something.txt","r"); // open for reading
if (!fp) printf ("file doesn''t exist");
else prinf("file exist");
fclose(fp);
}
请考虑以下事项:
%touch something.txt
%chmod -r something.txt
%a.out
文件不存在
那是不正确的。
Consider the following:
% touch something.txt
% chmod -r something.txt
% a.out
file doesn''t exist
That''s not correct.
这篇关于文件存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!