-   Programming (
-   -   catching the alphabets through getc during run-time. (

chansanle 12-31-2012 03:33 AM

catching the alphabets through getc during run-time.

will get the character from the terminal until a CTRL + D signal is passed.

With the help of getc, i can oly process the character after a enter is pressed i.e till the Enter key is pressed, it is keepon buffering the input :(

Is there any way to process each character during run-time i.e. before pressing Enter key ?

Thanks Chansanle

dwhitney67 12-31-2012 05:13 AM

You would need to enable non-blocking input to accomplish your goal. You can set this up yourself or rely on the ncurses library.

To set this up yourself, you would need to do something similar to the following:

#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>

#ifdef EOT
#undef EOT
#define EOT 4

 *  @fn non_blocking_input(int fileno, bool enable)
 *  @brief For enable or disabling non-blocking input on the specified stream.
 *  @param fileno - file number of stream to alter
 *  @param enable - set to true or false
int non_blocking_input(int fileno, bool enable)
    static struct termios old;

    if (enable)
        struct termios tmp;

        if (tcgetattr(fileno, &old))
            return -1;

        memcpy(&tmp, &old, sizeof(old));

        tmp.c_lflag &= ~ICANON & ~ECHO;

        if (tcsetattr(fileno, TCSANOW, (const struct termios*) &tmp))
            return -1;
        tcsetattr(fileno, TCSANOW, (const struct termios*) &old);

    return 0;

int main()
    // enable non-blocking input
    non_blocking_input(STDIN_FILENO, true);

    printf("Enter a sentence ending with a .:\n");

    int ch = EOT;

    // read single character until a '.' is read; a ctrl-D is interpreted as
    // an EOT (End Of Transmission).
    while (((ch = getchar()) != EOT) && (ch != '.'))
    if (ch != EOT)

    // re-enable blocking input
    non_blocking_input(STDIN_FILENO, false);

    return 0;

chansanle 01-04-2013 12:33 AM

Thanks for the support
hi dwhitney67,

Thanks a lot for your guidance and help. Now i came to know about the attributes of the terminal and how to manipulate using setattr and getattr. Will do the remainning job for the parser. Due to this non-blocking input, now i can implement a simple CLI parser :)

All times are GMT -5. The time now is 06:31 PM.