LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   function that reads characters without ener in c (https://www.linuxquestions.org/questions/programming-9/function-that-reads-characters-without-ener-in-c-4175490043/)

helpmaria 01-03-2014 04:36 PM

function that reads characters without ener in c
 
hello, I' m trying to find a function in c that could read a character when i type it, without having to press enter.
eg : if i'd like to read a string character by character
and after reading each one to check if the user typed '.' which function shall i use?
thank you.

astrogeek 01-03-2014 04:39 PM

man getc, getchar, etc...

*** EDIT ***

I did not see this was in the non*NIX forums, but still getc should be available, but not the man page.

dwhitney67 01-04-2014 08:28 PM

Quote:

Originally Posted by helpmaria (Post 5091435)
hello, I' m trying to find a function in c that could read a character when i type it, without having to press enter.
eg : if i'd like to read a string character by character
and after reading each one to check if the user typed '.' which function shall i use?
thank you.

To do this, you have at least two choices that I'm aware of... 1) use the ncurses library (which has getch()) or 2) fiddle with the terminal attributes so that input is non-blocking.

For choice 2, you can use something like is demonstrated in the following code:
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;
}


mina86 01-06-2014 03:46 AM

Quote:

Originally Posted by dwhitney67 (Post 5092053)
To do this, you have at least two choices that I'm aware of... […] fiddle with the terminal attributes so that input is non-blocking.

You are actually referring to input being “unbuffered”. “Non-blocking” means something different when it comes to reading and writing data, and in your example stdin stays blocking.

dwhitney67 01-06-2014 04:09 AM

Quote:

Originally Posted by mina86 (Post 5092721)
You are actually referring to input being “unbuffered”. “Non-blocking” means something different when it comes to reading and writing data, and in your example stdin stays blocking.

Yep, and some people say "tomayto", whereas others say "tomahto".

mina86 01-06-2014 04:48 AM

Quote:

Originally Posted by dwhitney67 (Post 5092736)
Yep, and some people say "tomayto", whereas others say "tomahto".

Your analogy is invalid since "tomayto" and "tomahto" mean the same thing, whereas “unbuffered” and “non-blocking” mean completely different and only slightly related things. “Unbuffered” means data as entered by the user for example is not buffered and transmitted to the application as soon as key is pressed (as opposed to waiting for the whole line for example), whereas “non-blocking” means calls to “read” will not block waiting for data if no data is available at the moment of the call. So please do not spread the confusion.


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