This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: getc() problem with Cygwin v1.0


Hallo Jim, 

at first in ANSI-C there is no function getc( void ). There is 
int getc( FILE *stream ) and int getchar( void ).

The problem is a little bit more complicated. getchar() is reading one character
from the input-stream and the function is not waiting for "RETURN". The question
is: When is a keystroke put in in the stream "stdin"?

The following little program shows this:

#include <stdio.h>
#include <string.h>

int main( void )
{
    char buffer[81];
    int i, ch;

    int re = 0;

   /*----------------------------------------*/

    printf( "Enter a line: " );

    /* Read in single line from "stdin": */
    for( i = 0; (i < 80) &&  ((ch = getc( stdin )) != EOF) 
             && (ch != '\n'); i++ )
    {
        printf( "!ch = %c\n", ch );
        buffer[i] = (char)ch;
    }

        /* Terminate string with null character: */
    buffer[i] = '\0';
    printf( "%s\n", buffer );


    printf( "READY, press Enter\n" );
    getchar();
    return re;
}

    /*--------------------*/


On my systems [ 1. Cygwin 20.1, 2. WindowsNT +  MinGw] the loop starts when the
"Return" is pressed and prints then one character at one time.

If you need to wait until "RETURN" is pressed maybe you have succes with
"gets()". Using "scanf()" is dangerous, because sometimes "RETURN" is left in
the buffer.

Dtcohen@aol.com wrote:
> 
> For Cygwin v1.0, getc() returns after every key is typed, instead
> of waiting for the carriage return.  This is incorrect behavior.
> With Cygwin B20.1 it correctly waits for the carriage return.
> 
> Does anyone know how this can be fixed?  Any pointers to the
> source of the problem are also appreciated.
> 
> Many thanks,
> Jim Grishaw
> dtcohen@aol.com

-- 
Georg Fusz

home-page: http://cadence.fb12.tu-berlin.de/~fusz/

Fon: 
Universitaet: +49 30 314 26 884
privat:     : +49 30 815 30 32
Handy:      : +49 173 20 10 696

--
Want to unsubscribe from this list?
Send a message to cygwin-unsubscribe@sourceware.cygnus.com


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]