Author: Gerd Isenberg
Date: 13:10:18 08/27/04
Go up one level in this thread
On August 27, 2004 at 16:04:20, Gerd Isenberg wrote:
>On August 27, 2004 at 15:41:33, Bas Hamstra wrote:
>
>>On August 27, 2004 at 15:07:28, Gerd Isenberg wrote:
>>
>>>On August 27, 2004 at 14:58:56, Gerd Isenberg wrote:
>>>
>>>>On August 27, 2004 at 14:28:06, Bas Hamstra wrote:
>>>>
>>>>>The following program gives me other output in XP than under previous windows
>>>>>versions:
>>>>>
>>>>>#include <stdio.h>
>>>>>#include <conio.h>
>>>>>
>>>>>int main(int argc, char* argv[])
>>>>>{ char Buf[16] = {0};
>>>>>
>>>>> setbuf(stdin, NULL);
>>>>>
>>>>> while(1)
>>>>> { if(kbhit() )
>>>>> { gets(Buf);
>>>>> printf("Received: %s\n", Buf);
>>>>> }
>>>>> }
>>>>> return 0;
>>>>>}
>>>>>
>>>>>Output:
>>>>>
>>>>>aaa
>>>>>Received: Paa
>>>>>
>>>>>So, if after typing aaa the first character seems to go lost. I am pretty
>>>>>certain that under Win98 it worked like intended. Is this a bug?
>>>>>
>>>>>(I am using XP with Borland C++ Builder 4.0)
>>>>>
>>>>>Bas.
>>>>
>>>>
>>>>with msc6 under XP i get
>>>>aaa (or any other sequence)
>>>>Received: d
>>>>
>>>>Seems that stdin does not work unbuffered.
>>>>
>>>>Gerd
>>>
>>>
>>>from
>>>
>>>http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/_crt_setbuf.asp
>>>
>>>...
>>>
>>>setbuf has been replaced by setvbuf, which is the preferred routine for new
>>>code. setbuf is retained for compatibility with existing code.
>>
>>Yes, that seems to solve the problem... Thanks Gerd.
>
>Seems setbuf is a bit depreciated.
>
>But setvbuf doesn't work unbuffered at all. If passing a NULL pointer,
>it allocates it's own buffer.
I should have read it to the end ;-(
----------------------------------------------------
Remarks
The setvbuf function allows the program to control both buffering and buffer
size for stream. stream must refer to an open file that has not undergone an I/O
operation since it was opened. The array pointed to by buffer is used as the
buffer, unless it is NULL, in which case setvbuf uses an automatically allocated
buffer of length size/2 * 2 bytes.
The mode must be _IOFBF, _IOLBF, or _IONBF. If mode is _IOFBF or _IOLBF, then
size is used as the size of the buffer. If mode is _IONBF, the stream is
unbuffered and size and buffer are ignored. Values for mode and their meanings
are:
_IOFBF
Full buffering; that is, buffer is used as the buffer and size is used as the
size of the buffer. If buffer is NULL, an automatically allocated buffer size
bytes long is used.
_IOLBF
With MS-DOS, the same as _IOFBF.
_IONBF
No buffer is used, regardless of buffer or size.
----------------------------------------------------
So unbuffered stdin does not work correctly under XP.
May that is solved with SP2 ;-)
Gerd
Your routine works well without any
>setbuf with default buffer too, or with setbuf(stdin, buf).
>Is there any special reason to switch off buffering here?
>
>Gerd
>
>
>
>>
>>Bas.
This page took 0 seconds to execute
Last modified: Thu, 15 Apr 21 08:11:13 -0700
Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.