LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-23-2011, 07:57 AM   #1
vijayavel
LQ Newbie
 
Registered: Aug 2011
Posts: 9

Rep: Reputation: Disabled
getche() in linux


how to use getche() function linux??????
AM newbie to linux c programming.
 
Old 08-23-2011, 10:47 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
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.

Last edited by tronayne; 08-23-2011 at 11:22 AM. Reason: Added getch() from The C Programming Langue
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
getche() and getch() kamransoomro84 Programming 2 05-09-2004 12:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration