Author: Bas Hamstra
Date: 14:14:17 08/27/04
Go up one level in this thread
On August 27, 2004 at 16:55:51, Bas Hamstra wrote:
>On August 27, 2004 at 16:06:52, Robert Hyatt 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.
>>
>>I suspect you are getting tangled up in buffering between the O/S, the C
>>library, and your program. Try using something unbuffered like read() and see
>>if it works OK.
>
>Yes, the code below seems to work fine. Still I am a bit confused *why* the
>earlier code does not work under XP...
>
>#include <stdio.h>
>#include <conio.h>
>#include <io.h>
>
>int main(int argc, char* argv[])
>{ char Buf[16] = {0};
>
> setbuf(stdin, NULL);
>
> while(1)
> { if(kbhit() )
> { //gets(Buf);
> read(fileno(stdin), Buf, 16);
> printf("Received: %s\n", Buf);
> }
> }
>
> return 0;
>}
>
>Thanks Bob,
>
>Bas.
To be precise, replacing
fgets(Buf, 128, stdin)
with
read(fileno(stdin), Buf, 128);
sscanf(Buf, "%[^\n]", Buf);
Seems to solve all problems. The sscanf strips the \n characters that the read()
picks up.
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.