ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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;
}
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.