fclose ...什么是更好的 [英] fclose... what is better

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

问题描述




最好这样做:


FILE * fp;

if((fp) = fopen(local_led," r"))!= NULL)

{

fclose(fp);

}

else

printf(fail\ n);




FILE * fp;

if((fp = fopen(local_led," r"))!= NULL)

{

printf(" oepn \ n");

}

其他

printf(" fail\\\
);

fclose (fp);


谢谢

hi

is it better to do:

FILE *fp;
if((fp = fopen(local_led, "r"))!=NULL)
{
fclose(fp);
}
else
printf("fail\n");
or

FILE *fp;
if((fp = fopen(local_led, "r"))!=NULL)
{
printf("oepn\n");
}
else
printf("fail\n");
fclose(fp);

thanks

推荐答案

文章< 11 *** *******************@z14g2000cwz.googlegroups .com> ;,

collinm< co ***** @ laboiteaprog.com>写道:
In article <11**********************@z14g2000cwz.googlegroups .com>,
collinm <co*****@laboiteaprog.com> wrote:
这样做更好:

FILE * fp;
if((fp = fopen(local_led," r"))!= NULL)
{
printf(" oepn\\\
");
}
其他
printf(" fail\\\
");
fclose(fp);
is it better to do:
or FILE *fp;
if((fp = fopen(local_led, "r"))!=NULL)
{
printf("oepn\n");
}
else
printf("fail\n");
fclose(fp);




fclose定义的行为是什么(NULL)?


如果你不''知道你的头脑,很可能是

维持你的程序的其他人也不会。因此,无论fclose(NULL)的定义行为如何,

,如果你没有

调用你的

程序将更加维护友好那个行为。

顺便说一句,作为一个小问题:像失败这样的消息

通常与错误有关,而不是正常的

程序行为。与错误相关的消息通常是发送到stderr而不是stdout的



如果文件的不可用性是程序的常规问题,

然后关联的消息通常不会失败。而是更接近跳过不可用的文件%s \ n的东西。或对不起,没有

今天闪烁的眨眼灯!\ n"

-

研究显示普通读者忽略了106所有统计数据的百分比

他们在.signatures中看到。



What is the defined behaviour of fclose(NULL) ?

If you don''t know off the top of your head, chances are that
other people maintaining your program will not either. Thus,
regardless of the defined behaviour of fclose(NULL), your
program would be more maintenance-friendly if you do not
invoke that behaviour.
By the way, as a minor matter of style: messages such as "fail"
are most often associated with errors rather than with normal
program behaviour. Messages associated with errors are usually
sent to stderr instead of stdout.

If the unavailability of the file is a routine matter for the program,
then the associated message would usually not be "fail" but instead
something closer to "skipping unavailable file %s\n" or "Sorry, no
winking blinking lights available today!\n"
--
Studies show that the average reader ignores 106% of all statistics
they see in .signatures.


collinm写道:
collinm wrote:
hi
<是不是更好:

FILE * fp;
if((fp = fopen(local_led," r"))!= NULL)
{
fclose(fp);
}
其他
printf(" fail\\\
);



FILE * fp;
if((fp = fopen(local_led," r"))!= NULL)
{
printf(" oepn \ n");
}

printf(fail\\\
);
fclose(fp);

谢谢
hi

is it better to do:

FILE *fp;
if((fp = fopen(local_led, "r"))!=NULL)
{
fclose(fp);
}
else
printf("fail\n");
or

FILE *fp;
if((fp = fopen(local_led, "r"))!=NULL)
{
printf("oepn\n");
}
else
printf("fail\n");
fclose(fp);

thanks




全部取决于你的风格和错误处理。

我的偏好是:

unsigned char successful = 0;

fp = fopen(...);

成功= fp == NULL;

如果(!成功)

{

/ * F. ile open failure * /

}


if(成功)

{

成功= Process_File_Contents ();

}


如果(成功)

{

/ * ... * /

}


至于何时关闭文件,我的意见是

一旦你关闭它就会关闭它完成它。

将文件打开超过必要的时间

可能允许内部数据发生的事情

{由您的程序或其他的)。


-

托马斯马修斯


C ++新闻组欢迎辞:
< a rel =nofollowhref =http://www.slack.net/~shiva/welcome.txttarget =_ blank> http://www.slack.net/~shiva/welcome.txt

C ++常见问题: http:// www .parashift.com / c ++ - faq-lite

C常见问题: http://www.eskimo.com/~s cs / c-faq / top.html

alt.comp.lang.learn.c-c ++ faq:
http://www.comeaucomputing.com/learn/faq/

其他网站:
http://www.josuttis.com - C ++ STL Library book
http://www.sgi.com/tech/stl - 标准模板库



All depends on your style and error handling.
My preference is:
unsigned char successful = 0;
fp = fopen(...);
successful = fp == NULL;
if (!successful)
{
/* File open failure */
}

if (successful)
{
successful = Process_File_Contents();
}

if (successful)
{
/* ... */
}

As for when to close a file, my opinion is to
close it as soon as you are finished with it.
Leaving a file open for longer than necessary
may allow things to happen to the internal data
{by your program or other ones).

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library




collinm写道:


collinm wrote:

hi

是不是更好:

FILE * fp;
if((fp = fopen(local_led," r"))!= NULL)
{
fclose(fp);
}

printf(fail\ n);


FILE * fp;
if((fp = fopen(local_led," r"))!= NULL)
{
printf(" oepn \ n");
}
其他
printf(" fail\\\
);
fcl OSE(FP);


如果fp为NULL,请不要调用fclose(fp)。

谢谢

hi

is it better to do:

FILE *fp;
if((fp = fopen(local_led, "r"))!=NULL)
{
fclose(fp);
}
else
printf("fail\n");

or

FILE *fp;
if((fp = fopen(local_led, "r"))!=NULL)
{
printf("oepn\n");
}
else
printf("fail\n");
fclose(fp);
Do not call fclose(fp) if fp is NULL.

thanks




- -

Fred L. Kleinschmidt

波音助理技术研究员

技术架构师,通用用户界面服务

M / S 2R-94(206)544-5225



--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Common User Interface Services
M/S 2R-94 (206)544-5225


这篇关于fclose ...什么是更好的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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