LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   getche() in linux (https://www.linuxquestions.org/questions/programming-9/getche-in-linux-898942/)

vijayavel 08-23-2011 07:57 AM

getche() in linux
 
how to use getche() function linux??????
AM newbie to linux c programming.

tronayne 08-23-2011 10:47 AM

Are you sure you want to use the getch() function? It's part of the curses library functions, not a typical new-to-C programming sort of thing; perhaps you're thinking of getc(), getchar() or maybe gets() functions?

The getch() function is a demonstration function found in The C Programming Language, both the first and second editions, on page 79 (yup, same page in both editions). It's not something that you'd actually use in practice, but here it is:
Code:

#include <stdio.h>              /* needed for printf) */

#define BUFSIZE 100

char    buf [BUFSIZE];          /* buffer for ungetch() */
int    bufptr = 0;            /* next free position in buf */

int    getch (void)            /* get a (possibly pushed back) character */
{
        return (bufptr > 0) ? buf [--bufptr] : getchar ();
}

void    ungetch (int c)        /* push character back on input */
{
        if (bufptr >= BUFSIZE)
                printf ("ungetch: too many characters\n");
        else
                buf [bufptr++] = c;
        return;
}

Bear in mind that you would not actually use this function; it's for illustration only.

Here's a small example of getchar():
Code:

#include <stdio.h>
#include <stdlib.h>

int    main    (void)
{
        int    c;

        while ((c = getchar ()) != EOF)    /* EOF is Ctrl^D */
                (void) putchar (c);
        exit (EXIT_SUCCESS);
}

And getc():
Code:

#include <stdio.h>
#include <stdlib.h>

int    main    (void)
{
        int    c;
        FILE    *helpfile;    /* create a file named "instructions" with some text in it */

        if ((helpfile = fopen ("instructions", "r")) == (FILE *) NULL) {
                (void) fprintf (stderr, "Can't append to instructions\n");
                exit (EXIT_FAILURE);
        }
        while ((c = getc (helpfile)) != EOF)
                (void) putchar (c);
        (void) fclose (helpfile);
        exit (EXIT_SUCCESS);
}

And, finally, gets() (this function is depreciated and shouldn't really be used; use fgets() instead):
Code:

#include <stdio.h>
#include <stdlib.h>

int    main    (void)
{
        char    buf [BUFSIZ];
        FILE    *temp;

        if ((temp = tmpfile ()) == (FILE *) NULL) {
                (void) fprintf (stderr, "can't create temp file\n");
                exit (EXIT_FAILURE);
        }
        (void) fputs ("Some data written to a temporary file!\n", temp);
        (void) rewind (temp);
        (void) fgets (buf, sizeof (buf), temp);
        (void) fputs (buf, stdout);
        exit (EXIT_SUCCESS);
}

See the manual page for getc for more information about this family of functions.

Hope this helps some.


All times are GMT -5. The time now is 11:01 PM.