user-0 提问: 菜鸟求解释缓冲区和fflush函数

回复:
user: user-1
content: 没会什么直接的方法,只有张 stdin缓冲区的数据读取出来,再输出 简单点这样就可以了  int main(int argc, char *argv[]) { 	char c; 	getchar(); 	 	while ( 10 != (c = getchar()) )    // 10 : 输入结束/换行符 		putchar(c);  	return 0; }

user: user-0
content: 引用 7 楼 dooX8086 的回复:缓冲区是独立的. 这样不理解么~? getchar();  // 缓冲区张三 ----> stdin fflush( stdout); // 缓冲区李四 ----> stdout  ----------------------------------------------------------------- fflush(stdout)  // 叫缓冲区李四回家吃饭,  所以....缓冲张三还在网吧...  如果想要实现我原本的意图，该如何做。 将剩余的在屏幕上显示出来

user: user-0
content: 引用 7 楼 dooX8086 的回复:缓冲区是独立的. 这样不理解么~? getchar();  // 缓冲区张三 ----> stdin fflush( stdout); // 缓冲区李四 ----> stdout  ----------------------------------------------------------------- fflush(stdout)  // 叫缓冲区李四回家吃饭,  所以....缓冲张三还在网吧... 赞！！！！！！！！！！！！！！ 意思stdin和stdout，占用了两个不同的缓冲区是吧

user: user-1
content: 缓冲区是独立的. 这样不理解么~? getchar();  // 缓冲区张三 ----> stdin fflush( stdout); // 缓冲区李四 ----> stdout  ----------------------------------------------------------------- fflush(stdout)  // 叫缓冲区李四回家吃饭,  所以....缓冲张三还在网吧...

user: user-0
content: 引用 5 楼 dooX8086 的回复:文件(句柄)缓冲区是"私有""的.如 fopen(A),  fopen(B)./ soket(A) socket(B) 等... 会产生两个独立的 A  B缓冲区.... 而 fflush 将缓冲的数据写回自己的文件(句柄) 没看懂，请问这个什么情况 #include<stdio.h> int main() { 	    getchar();    //输入“abcdef”，缓冲区产生亢余 		fflush(stdout);   //预想输出“bcdef”，实际输出为NULL 		return 0; }

user: user-1
content: 文件(句柄)缓冲区是"私有""的.如 fopen(A),  fopen(B)./ soket(A) socket(B) 等... 会产生两个独立的 A  B缓冲区.... 而 fflush 将缓冲的数据写回自己的文件(句柄)

user: user-0
content: 引用 3 楼 my_live_123 的回复:你打开一个文件，没有执行相应的读写操作，也就不存在对应的缓冲区。你fflush当然会失败！因此在打开一个文件后，并没有给你分配缓冲区，这这个时间点上，其实给用户一个改变缓冲区大小的权利！你可以尝试对m进行一些读写操作，然后再fflush它 能否帮我看看一楼那个程序。

user: user-2
content: 你打开一个文件，没有执行相应的读写操作，也就不存在对应的缓冲区。你fflush当然会失败！因此在打开一个文件后，并没有给你分配缓冲区，这这个时间点上，其实给用户一个改变缓冲区大小的权利！你可以尝试对m进行一些读写操作，然后再fflush它

user: user-3
content: fflush Flushes a stream.  int fflush( FILE *stream );  Function Required Header Compatibility  fflush <stdio.h> ANSI, Win 95, Win NT    For additional compatibility information, see Compatibility in the Introduction.  Libraries  LIBC.LIB Single thread static library, retail version  LIBCMT.LIB Multithread static library, retail version  MSVCRT.LIB Import library for MSVCRT.DLL, retail version    Return Value  fflush returns 0 if the buffer was successfully flushed. The value 0 is also returned in cases in which the specified stream has no buffer or is open for reading only. A return value of EOF indicates an error.  Note   If fflush returns EOF, data may have been lost due to a write failure. When setting up a critical error handler, it is safest to turn buffering off with the setvbuf function or to use low-level I/O routines such as _open, _close, and _write instead of the stream I/O functions.  Parameter  stream  Pointer to FILE structure  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.  Buffers are normally maintained by the operating system, which determines the optimal time to write the data automatically to disk: when a buffer is full, when a stream is closed, or when a program terminates normally without closing the stream. The commit-to-disk feature of the run-time library lets you ensure that critical data is written directly to disk rather than to the operating-system buffers. Without rewriting an existing program, you can enable this feature by linking the program’s object files with COMMODE.OBJ. In the resulting executable file, calls to _flushall write the contents of all buffers to disk. Only _flushall and fflush are affected by COMMODE.OBJ.  For information about controlling the commit-to-disk feature, see Stream I/O, fopen, and _fdopen.  Example  /* FFLUSH.C */  #include <stdio.h> #include <conio.h>  void main( void ) {    int integer;    char string[81];     /* 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\n", string ); }   Output  Enter a sentence of four words with scanf: This is a test This is a test Enter the same sentence with gets: This is a test This is a test   Stream I/O Routines  See Also   fclose, _flushall, setvbuf

user: user-0
content: 另一个例子，依旧失败： #include<stdio.h> #include<stdlib.h> int main() { 	FILE *p,*m; 	char n; 		m=fopen("M","w"); 		n=getchar();  //输入“aaaaaaaa”,键盘输入产生缓冲区亢余 		fflush(m);      //想要将剩余的字符传入给m文件 		return 0; }