免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2510 | 回复: 6
打印 上一主题 下一主题

[函数] 请教函数fflush [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2003-12-11 09:29 |只看该作者 |倒序浏览
Remarks

The fflush function flushes a stream. If the file associated with stream is open for output, fflush writes to that file the contents of the buffer associated with the stream. If the stream is open for input, fflush clears the contents of the buffer. fflush negates the effect of any prior call to ungetc against stream. Also, fflush(NULL) flushes all streams opened for output. The stream remains open after the call. fflush has no effect on an unbuffered stream.
这时什么意思?

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
2 [报告]
发表于 2003-12-11 09:37 |只看该作者

请教函数fflush

也就是说如果是输出流,就把流缓冲区的内容写到和这个流关联的文件
里。如果是输入流,就把和输入关联的流缓冲区的内容清除。当然,它只对有缓冲区的流有作用,如果是unbuffered stream,没有影响。

论坛徽章:
0
3 [报告]
发表于 2003-12-11 09:56 |只看该作者

请教函数fflush

去掉或加上fflush(stdou);行,
下面的代码一试便知

  1. #include <stdio.h>;


  2. int main()
  3. {
  4.         printf("abcd");
  5.         fflush(stdout);
  6.         pause();
  7. }
复制代码

论坛徽章:
0
4 [报告]
发表于 2003-12-11 10:11 |只看该作者

请教函数fflush

对,我也发现
#include <stdio.h>;
#include <conio.h>;

void main( void )
{
   int integer;
   char string[81];

   string[0]='y';
   string[1]='z';
   /* Read each word as a string. */
   printf( "Enter a sentence of four words with scanf: " );
   for( integer = 0; integer < 4; integer++ )
   {
      scanf( "%s", string );
      printf( "%s\n", string );
   }

   /* You must flush the input buffer before using gets. */
   fflush( stdin );
   printf( "Enter the same sentence with gets: " );
   gets(string);   
   printf( "%s  %c\n", string ,string[1]);
}

#include <stdio.h>;
#include <conio.h>;

void main( void )
{
   int integer;
   char string[81];

   string[0]='y';
   string[1]='z';
   /* Read each word as a string. */
   printf( "Enter a sentence of four words with scanf: " );
   for( integer = 0; integer < 4; integer++ )
   {
      scanf( "%s", string );
      printf( "%s\n", string );
   }

   /* You must flush the input buffer before using gets. */
   //fflush( stdin );
   printf( "Enter the same sentence with gets: " );
   //gets(string);   
   scanf( "%s", string );
   
   printf( "%s  %c\n", string ,string[1]);
}
这两个程序输出一样,而#include <stdio.h>;
#include <conio.h>;

void main( void )
{
   int integer;
   char string[81];

   string[0]='y';
   string[1]='z';
   /* Read each word as a string. */
   printf( "Enter a sentence of four words with scanf: " );
   for( integer = 0; integer < 4; integer++ )
   {
      scanf( "%s", string );
      printf( "%s\n", string );
   }

   /* You must flush the input buffer before using gets. */
   //fflush( stdin );
   printf( "Enter the same sentence with gets: " );
   gets(string);   
  // scanf( "%s", string );
   
   printf( "%s  %c\n", string ,string[1]);
}
就不一样了,我还是不懂??
这里还牵扯到scanf与gets的区别吧

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
5 [报告]
发表于 2003-12-11 13:20 |只看该作者

请教函数fflush

知识flush缓存与否,前提是运行环境是缓存的

论坛徽章:
0
6 [报告]
发表于 2003-12-11 13:31 |只看该作者

请教函数fflush

对,其实lenovo 已经讲清楚了,只是我还是不理解函数fflush的具体含义和作用或者内部时如何处理的

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
7 [报告]
发表于 2003-12-11 14:40 |只看该作者

请教函数fflush

[quote]原帖由 "kingld"]对,其实lenovo 已经讲清楚了,只是我还是不理解函数fflush的具体含义和作用或者内部时如何处理的[/quote 发表:

建议你看《unix环境高级编程》,大概是第四章。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP