user-0 提问: 关于setbuf的问题

回复:
user: user-1
content: 查MSDN或者man setbuf 不按规则出牌，即使（暂时的）结果是你想要的，也不能说明什么问题， 更何况出现跟预期的结果不一样。

user: user-2
content: 引用 5 楼 u011579318 的回复:[quote=引用 4 楼 zhao4zhong1 的回复:] setbuf Controls stream buffering.  void setbuf( FILE *stream, char *buffer );  Routine Required Header Compatibility  setbuf <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  None  Parameters  stream  Pointer to FILE structure  buffer  User-allocated buffer  Remarks  The setbuf function controls buffering for stream. The stream argument must refer to an open file that has not been read or written. If the buffer argument is NULL, the stream is unbuffered. If not, the buffer must point to a character array of length BUFSIZ, where BUFSIZ is the buffer size as defined in STDIO.H. The user-specified buffer, instead of the default system-allocated buffer for the given stream, is used for I/O buffering. The stderr stream is unbuffered by default, but you can use setbuf to assign buffers to stderr.  setbuf has been replaced by setvbuf, which is the preferred routine for new code. setbuf is retained for compatibility with existing code.  Example  /* SETBUF.C: This program first opens files named DATA1 and  * DATA2. Then it uses setbuf to give DATA1 a user-assigned  * buffer and to change DATA2 so that it has no buffer.  */  #include <stdio.h>  void main( void ) {    char buf[BUFSIZ];    FILE *stream1, *stream2;     if( ((stream1 = fopen( "data1", "a" )) != NULL) &&        ((stream2 = fopen( "data2", "w" )) != NULL) )    {       /* "stream1" uses user-assigned buffer: */       setbuf( stream1, buf );       printf( "stream1 set to user-defined buffer at: %Fp\n", buf );        /* "stream2" is unbuffered              */       setbuf( stream2, NULL );       printf( "stream2 buffering disabled\n" );       _fcloseall();    } }   Output  stream1 set to user-defined buffer at: 0013FDA0 stream2 buffering disabled   Stream I/O Routines  See Also   fclose, fflush, fopen, setvbuf 我今天发现我在windows虚拟机上用vc编译器得到的是我想要的结果，但是在ubuntu下使用codeblocks或者qt得到的都是上述的奇怪结果，但是我暂时不会在ubuntu下跟到puts里面去，因为提示无法找到puts.c。[/quote] 体会到了ubuntu要跟上windows的步伐还有很长的路要走了吧。

user: user-0
content: 引用 4 楼 zhao4zhong1 的回复:setbuf Controls stream buffering.  void setbuf( FILE *stream, char *buffer );  Routine Required Header Compatibility  setbuf <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  None  Parameters  stream  Pointer to FILE structure  buffer  User-allocated buffer  Remarks  The setbuf function controls buffering for stream. The stream argument must refer to an open file that has not been read or written. If the buffer argument is NULL, the stream is unbuffered. If not, the buffer must point to a character array of length BUFSIZ, where BUFSIZ is the buffer size as defined in STDIO.H. The user-specified buffer, instead of the default system-allocated buffer for the given stream, is used for I/O buffering. The stderr stream is unbuffered by default, but you can use setbuf to assign buffers to stderr.  setbuf has been replaced by setvbuf, which is the preferred routine for new code. setbuf is retained for compatibility with existing code.  Example  /* SETBUF.C: This program first opens files named DATA1 and  * DATA2. Then it uses setbuf to give DATA1 a user-assigned  * buffer and to change DATA2 so that it has no buffer.  */  #include <stdio.h>  void main( void ) {    char buf[BUFSIZ];    FILE *stream1, *stream2;     if( ((stream1 = fopen( "data1", "a" )) != NULL) &&        ((stream2 = fopen( "data2", "w" )) != NULL) )    {       /* "stream1" uses user-assigned buffer: */       setbuf( stream1, buf );       printf( "stream1 set to user-defined buffer at: %Fp\n", buf );        /* "stream2" is unbuffered              */       setbuf( stream2, NULL );       printf( "stream2 buffering disabled\n" );       _fcloseall();    } }   Output  stream1 set to user-defined buffer at: 0013FDA0 stream2 buffering disabled   Stream I/O Routines  See Also   fclose, fflush, fopen, setvbuf 我今天发现我在windows虚拟机上用vc编译器得到的是我想要的结果，但是在ubuntu下使用codeblocks或者qt得到的都是上述的奇怪结果，但是我暂时不会在ubuntu下跟到puts里面去，因为提示无法找到puts.c。

user: user-2
content: setbuf Controls stream buffering.  void setbuf( FILE *stream, char *buffer );  Routine Required Header Compatibility  setbuf <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  None  Parameters  stream  Pointer to FILE structure  buffer  User-allocated buffer  Remarks  The setbuf function controls buffering for stream. The stream argument must refer to an open file that has not been read or written. If the buffer argument is NULL, the stream is unbuffered. If not, the buffer must point to a character array of length BUFSIZ, where BUFSIZ is the buffer size as defined in STDIO.H. The user-specified buffer, instead of the default system-allocated buffer for the given stream, is used for I/O buffering. The stderr stream is unbuffered by default, but you can use setbuf to assign buffers to stderr.  setbuf has been replaced by setvbuf, which is the preferred routine for new code. setbuf is retained for compatibility with existing code.  Example  /* SETBUF.C: This program first opens files named DATA1 and  * DATA2. Then it uses setbuf to give DATA1 a user-assigned  * buffer and to change DATA2 so that it has no buffer.  */  #include <stdio.h>  void main( void ) {    char buf[BUFSIZ];    FILE *stream1, *stream2;     if( ((stream1 = fopen( "data1", "a" )) != NULL) &&        ((stream2 = fopen( "data2", "w" )) != NULL) )    {       /* "stream1" uses user-assigned buffer: */       setbuf( stream1, buf );       printf( "stream1 set to user-defined buffer at: %Fp\n", buf );        /* "stream2" is unbuffered              */       setbuf( stream2, NULL );       printf( "stream2 buffering disabled\n" );       _fcloseall();    } }   Output  stream1 set to user-defined buffer at: 0013FDA0 stream2 buffering disabled   Stream I/O Routines  See Also   fclose, fflush, fopen, setvbuf

user: user-0
content: 引用 2 楼 user-3 的回复:This function should be called once the stream has been associated with an open file, but before any input or output operation is performed with it. which function?

user: user-3
content: This function should be called once the stream has been associated with an open file, but before any input or output operation is performed with it.

user: user-2
content: http://www.microsoft.com/visualstudio/chs/downloads#d-2010-express 点开Visual C++ 2010 Express下面的语言选‘简体中文’，再点立即安装  puts有对应源代码的C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\puts.c  自己跟进去看。