LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 12-31-2012, 03:33 AM   #1
chansanle
LQ Newbie
 
Registered: May 2011
Posts: 22

Rep: Reputation: Disabled
catching the alphabets through getc during run-time.


while(getc(stdin)!=EOF);

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
 
Old 12-31-2012, 05:13 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,522

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
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:
Code:
#include <stdio.h>
#include <termios.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>

#ifdef EOT
#undef EOT
#endif
#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;
        }
    }
    else
    {
        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 != '.'))
    {
        putchar(ch);
    }
    if (ch != EOT)
    {
        puts(".");
    }

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

    return 0;
}
 
Old 01-04-2013, 12:33 AM   #3
chansanle
LQ Newbie
 
Registered: May 2011
Posts: 22

Original Poster
Rep: Reputation: Disabled
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] segfault with getc Gortex Programming 5 06-07-2011 01:59 PM
Checking for Alphabets gregarion Programming 1 01-25-2010 02:27 PM
Problem when use getc function before creating a new process with execve() anuj_sri Linux - Newbie 1 02-20-2008 08:29 PM
C getc without CR astropirhana Programming 5 09-01-2006 04:03 PM
Please teach me how to use getc(stdin) Eileen Programming 12 12-20-2004 12:34 AM


All times are GMT -5. The time now is 09:02 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration