关于我看到的一些C代码的问题 [英] Question on some C code that I saw

查看:56
本文介绍了关于我看到的一些C代码的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人有以下


#include< stdio.h>


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

{

FILE * fp = NULL;

char buffer [8];


/ *在这里插入argc / argv检查代码* /


fp = fopen(argv [1]," w");

if(fp! = NULL)

{

while(fgets(buffer,sizeof buffer,stdin))

{

printf ("%s",buffer);

fprintf(fp,"%s",buffer);

}

fflush( stdout);

fflush(fp);

fclose(fp);

}

else

{

fprintf(stderr,Broken tee.\ n);

}

返回0;


}

他们为什么叫fllush()?当你用
调用fclose()时,我认为flush()被调用了。这是编码错误吗?如果没有,为什么在从stdin读取时调用fflush()




Chad

Someone had the following

#include <stdio.h>

int main(int argc, char *argv[])
{
FILE *fp = NULL;
char buffer[8];

/* insert argc/argv checking code here */

fp = fopen(argv[1], "w");
if(fp != NULL)
{
while(fgets(buffer, sizeof buffer, stdin))
{
printf("%s", buffer);
fprintf(fp, "%s", buffer);
}
fflush(stdout);
fflush(fp);
fclose(fp);
}
else
{
fprintf(stderr, "Broken tee.\n");
}
return 0;

}
Why did they call fllush()? I thought that flush() got called when you
called fclose(). Is this a coding error? If not, why call fflush()
when reading from stdin.

Chad

推荐答案

Chad< cdal ... @ gmail.comwrote:
Chad <cdal...@gmail.comwrote:

有人有以下
Someone had the following



如果引用他的代码,理查德不会感到尴尬。确实,

他可能更喜欢你在上下文中引用它。

Richard won''t be embarrased if you quote his code. Indeed,
he may prefer that you quote it in context.


#include< stdio.h>


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

{

FILE * fp = NULL;
#include <stdio.h>

int main(int argc, char *argv[])
{
FILE *fp = NULL;



[这是关于作者是谁的赠品,但

我向谷歌证实了这一点。 ; - ]

[This is the giveaway line as to who the author was, but
I confirmed it with Google. ;-]


char buffer [8];
char buffer[8];



原版有80而不是8,虽然它不会改变

语义。

The original had 80 not 8, though it won''t change the
semantics.


/ *在这里插入argc / argv检查代码* /


fp = fopen(argv [1]," w");

if(fp!= NULL)

{

while(fgets(buffer,sizeof buffer,stdin))

{

printf("%s",buffer);

fprintf(fp,"%s",buffer);

}

fflush(stdout);

fflush(fp);

fclose(fp);

}

其他

{

fprintf(stderr,Broken tee.\ n);

}

返回0;


}


为什么他们调用fllush()?
/* insert argc/argv checking code here */

fp = fopen(argv[1], "w");
if(fp != NULL)
{
while(fgets(buffer, sizeof buffer, stdin))
{
printf("%s", buffer);
fprintf(fp, "%s", buffer);
}
fflush(stdout);
fflush(fp);
fclose(fp);
}
else
{
fprintf(stderr, "Broken tee.\n");
}
return 0;

}

Why did they call fllush()?



ITYM fflush()

ITYM fflush()


我认为flush()
I thought that flush()



ITYM fflush()

ITYM fflush()

当你调用fclose()时调用

got called when you called fclose().



是。

Yes.


这是编码错误吗?
Is this a coding error?



No.

No.


如果没有,为什么要调用fflush()
If not, why call fflush()



对于fp,没有任何要求,因为它已经关闭了

之后。理查德的理由可能很美。


但是对于stdout,最后一行读取不需要用换行符来终止

。在许多实现中,非换行

终止文本不会出现在stdout控制台上,直到它刷新

For fp, there''s no requirement since it''s closed straight
afterwards. Richard''s reasons are probably aesthetic.

But for stdout, the last line read need not be terminated
with a newline. On many implementations, non-newline
terminated text won''t appear on a stdout console until it
is flushed.



when reading from stdin.



这是偶然的。 fflush()通常只适用于输出

流,并且不依赖于您正在读取的任何输入流(或

流)。


-

彼得

That is incidental. fflush() generally only works on output
streams and is not dependant on whatever input stream (or
streams) you happen to be reading from.

--
Peter


文章

< db ***** ******** @ k62g2000hse。 googlegroups.com>,

Chad< cd ***** @ gmail.comwrote于2007年11月15日星期四上午8:34:
In article
<db**********************************@k62g2000hse. googlegroups.com>,
Chad <cd*****@gmail.comwrote on Thursday 15 Nov 2007 8:34 am:

有人有以下


#include< stdio.h>


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

{

FILE * fp = NULL;

char buffer [8];


/ *在这里插入argc / argv检查代码* /


fp = fopen(argv [1]," w");

if(fp!= NULL)

{

while(fgets(buffer,sizeof buffer,stdin))

{

printf( "%s",buffer);

fprintf(fp,"%s",buffer);

}

fflush(stdout );

fflush(fp);

fclose(fp);

}

其他

{

fprintf(stderr,Broken tee.\ n);

}

返回0;


}


为什么他们打电话给fllu SH()?当你用
调用fclose()时,我认为flush()被调用了。这是编码错误吗?如果没有,为什么在从stdin读取时调用fflush()


Someone had the following

#include <stdio.h>

int main(int argc, char *argv[])
{
FILE *fp = NULL;
char buffer[8];

/* insert argc/argv checking code here */

fp = fopen(argv[1], "w");
if(fp != NULL)
{
while(fgets(buffer, sizeof buffer, stdin))
{
printf("%s", buffer);
fprintf(fp, "%s", buffer);
}
fflush(stdout);
fflush(fp);
fclose(fp);
}
else
{
fprintf(stderr, "Broken tee.\n");
}
return 0;

}
Why did they call fllush()? I thought that flush() got called when you
called fclose(). Is this a coding error? If not, why call fflush()
when reading from stdin.



当通过显式调用fclose()或通过正常程序终止隐式地使用
关闭流时,所有它的缓冲区满脸通红。但

有时需要确保数据立即写入

设备。等到流关闭,或直到

程序终止,或直到缓冲区填满,可能是不可接受的。

在这种情况下,可以使用fflush()。另一种方法是使用setvbuf()关闭流来缓冲

,但这可能不足以支持



注意fflush(stdin)是未定义的行为。


该程序对我来说似乎没有多大意义。究竟它应该完成什么?b $ b?在将缓冲区发送到''fp''

和''stdout''之前,它必须至少删除由fgets()写的null

字符。另外我认为fflush()的两个调用应该在内部,

不在外面,while循环。它也会返回成功状态。终止

,无论程序的实际结果如何。

When a stream is closed via an explicit call to fclose() or implicitly
by normal program termination, all it''s buffers are flushed. But
sometimes it is necessary to make sure that the data is written to the
device immediately. Waiting till the stream is closed, or till the
program terminates, or till the buffers fill up, may be unacceptable.
In such situations fflush() can be employed. One alternative is to turn
off buffering for the stream by using setvbuf(), but this may not be
quite enough.

Note that fflush(stdin) is undefined behaviour.

The program doesn''t seem to make a lot of sense to me. What exactly is
it supposed to accomplish? At a minimum it must remove the null
character written by fgets() before sending the buffer to ''fp''
and ''stdout''. Also I think the two calls to fflush() should be within,
not outside, the while loop. Also it returns "successful" termination
for regardless of the program''s actual outcome.


2007年11月14日星期三19:04:18 -0800(PST),Chad< cd ***** @ gmail.com>

在comp.lang.c中写道:
On Wed, 14 Nov 2007 19:04:18 -0800 (PST), Chad <cd*****@gmail.com>
wrote in comp.lang.c:

有人有以下


#include< stdio.h>


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

{

FILE * fp = NULL;

char buffer [8];


/ *在这里插入argc / argv检查代码* /


fp = fopen(argv [1]," w");

if(fp!= NULL)

{

while(fgets(buffer,sizeof buffer,stdin))

{

printf( "%s",buffer);

fprintf(fp,"%s",buffer);

}

fflush(stdout );

fflush(fp);
Someone had the following

#include <stdio.h>

int main(int argc, char *argv[])
{
FILE *fp = NULL;
char buffer[8];

/* insert argc/argv checking code here */

fp = fopen(argv[1], "w");
if(fp != NULL)
{
while(fgets(buffer, sizeof buffer, stdin))
{
printf("%s", buffer);
fprintf(fp, "%s", buffer);
}
fflush(stdout);
fflush(fp);



单次调用fflush(NULL)可以替换上面的两个调用,

完全相同的结果。

A single call to fflush(NULL) could replace both calls above, with
exactly the same result.


fclose(fp);

}

else

{

fprintf(stderr,Broken tee.\ n);

}

返回0;


}


为什么他们调用fllush()?当你用
调用fclose()时,我认为flush()被调用了。这是编码错误吗?如果没有,为什么在从stdin读取时调用fflush()


fclose(fp);
}
else
{
fprintf(stderr, "Broken tee.\n");
}
return 0;

}
Why did they call fllush()? I thought that flush() got called when you
called fclose(). Is this a coding error? If not, why call fflush()
when reading from stdin.



在有效的文件指针上调用fflush(NULL)绝不是错误

为输出打开而未关闭。


至于为什么,一个不知道自己在做什么的程序员,或者一个不太可能的
,一个破损的图书馆。


但是是什么让你觉得从stdin读取有什么事可做?

with?

It is never an error to call fflush(NULL) on a valid file pointer
opened for output and not closed.

As to why, either a programmer who does not know what he is doing or,
much less likely, a broken library.

But what makes you think that reading from stdin has anything to do
with is?


Chad
Chad



-

Jack Klein

主页: http://JK-Technology.Com

常见问题解答

comp.lang.c http://c-faq.com/

comp.lang.c ++ < a rel =nofollowhref =http://www.parashift.com/c++-faq-lite/target =_ blank> http://www.parashift.com/c++-faq-lite/

alt.comp.lang.learn.c-c ++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html


这篇关于关于我看到的一些C代码的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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