当fopen()失败时 [英] When fopen() fails

查看:73
本文介绍了当fopen()失败时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果fopen失败,有没有办法知道原因?

解决方案

Sathyaish< Vi ********** ******@yahoo.com>这样说:

如果fopen失败,有没有办法知道原因?




如果通过失败你的意思是返回NULL,是的。全局变量errno

(在< errno.h>中找到)包含有关出错的信息;你可以使用perror()将这些信息打印成可读的字符串。


-

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

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。


在文章< cj ********** @ chessie.cirr.com>,

克里斯托弗Benson-Manica< at *** @ nospam.cyberspace.org>写道:

Sathyaish< Vi **************** @ yahoo.com>这样说:

如果fopen失败,有没有办法知道原因?
如果通过失败你的意思是返回NULL,是的。全局变量errno
(在< errno.h>中找到)包含有关出错的信息;你可以用perror()把这些信息打印成一个可读的字符串。




如果我没弄错的话,fopen并不需要设置errno,如果它失败了。

(虽然大多数试图提供非零QoI的实现都会安排

安排它设置有用。)

dave


-

Dave Vandervies dj ** ****@csclub.uwaterloo.ca 声称ANSI C不允许键盘上的奇怪作者是错误的。



感谢您的澄清。在那里,我害怕我必须找到其他工作。 --Ben Pfaff和Michael M Rubenstein在comp.lang.c


Vi **************** @ yahoo.com (Sathyaish)写道:

如果fopen失败,有没有办法知道原因?




您可以检查错误或查看相应的消息

由strerror返回()。它不能保证可移植,

但合理的实现将提供有用的信息。

-

一位称职的C程序员知道如何正确编写C程序,

a C专家知道足以与Dan Pop争论,而C专家

专家知道不要打扰。


If fopen fails, is there a way to know why?

解决方案

Sathyaish <Vi****************@yahoo.com> spoke thus:

If fopen fails, is there a way to know why?



If by "fails" you mean "returns NULL", yes. The global variable errno
(found in <errno.h>) contains information about what went wrong; you
can use perror() to print that information as a readable string.

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.


In article <cj**********@chessie.cirr.com>,
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:

Sathyaish <Vi****************@yahoo.com> spoke thus:

If fopen fails, is there a way to know why?
If by "fails" you mean "returns NULL", yes. The global variable errno
(found in <errno.h>) contains information about what went wrong; you
can use perror() to print that information as a readable string.



If I''m not mistaken, fopen isn''t required to set errno if it fails.
(Though most implementations that attempt to provide nonzero QoI do
arrange for it to be set usefully.)
dave

--
Dave Vandervies dj******@csclub.uwaterloo.caClaiming that ANSI C does not allow a weird author at the keyboard is wrong.


Thanks for the clarification. For a moment there I was afraid I''d have to find
other work. --Ben Pfaff and Michael M Rubenstein in comp.lang.c


Vi****************@yahoo.com (Sathyaish) writes:

If fopen fails, is there a way to know why?



You can examine errno or look at the corresponding message
returned by strerror(). It''s not guaranteed to work portably,
but reasonable implementations will provide a useful message.
--
A competent C programmer knows how to write C programs correctly,
a C expert knows enough to argue with Dan Pop, and a C expert
expert knows not to bother.


这篇关于当fopen()失败时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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