LinuxQuestions.org
Review your favorite Linux distribution.
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 01-03-2014, 04:36 PM   #1
helpmaria
LQ Newbie
 
Registered: Jan 2014
Posts: 1

Rep: Reputation: Disabled
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.
 
Old 01-03-2014, 04:39 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

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

Last edited by astrogeek; 01-03-2014 at 04:40 PM.
 
Old 01-04-2014, 08:28 PM   #3
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by helpmaria View Post
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;
}
 
Old 01-06-2014, 03:46 AM   #4
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by dwhitney67 View Post
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.
 
Old 01-06-2014, 04:09 AM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by mina86 View Post
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".

Last edited by dwhitney67; 01-06-2014 at 04:10 AM.
 
Old 01-06-2014, 04:48 AM   #6
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by dwhitney67 View Post
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.
 
  


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
php header() function and hidden characters hakansukur Programming 8 08-04-2011 02:30 PM
Difference between buffered disk reads and cached reads? pinga123 Linux - Newbie 1 07-13-2011 11:40 AM
LXer: Mastering Characters Sets in Linux (Weird Characters, part 2) LXer Syndicated Linux News 0 11-25-2009 11:30 PM
Parenthesis and other odd characters passed to bash function jakeo25 Programming 2 04-03-2008 10:27 AM
How to modify the names of files and replace characters with other characters or symb peter88 Linux - General 2 12-10-2006 03:05 AM

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

All times are GMT -5. The time now is 07:25 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