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.
The inbuilt buffer delmiter will be "\n" i.e, only after a "\n" is encountered the input will be accepted by the program...Until then it'll be stored in the buffer . On receiving a carriage return it is sent to the program which requested it....
I want to write a c-program which takes input as they come i.e,
if the user types "a" I must accept it even if he doesn't type "\n"
Maybe u should read the problem statement again
Using getchar(),
The output will be printed line by line i.e, you enter a line and then a line is printed , then another line is accepted,printed....
This is because the delimiter of internal buffer is "\n" ...
#include <stdio.h>
#include <termios.h>
static struct termios stored_settings;
void set_keypress(void)
{
struct termios new_settings;
tcgetattr(0,&stored_settings);
new_settings = stored_settings;
new_settings.c_lflag &= (~ICANON);
new_settings.c_cc[VTIME] = 0;
new_settings.c_cc[VMIN] = 1;
tcsetattr(0,TCSANOW,&new_settings);
}
void reset_keypress(void)
{
tcsetattr(0,TCSANOW,&stored_settings);
return;
}
int main()
{
char c;
int i;
/* Call function to set terminal to buffer by only one char
/* and deliver it inmediately, so we can now use getchar().
*/
set_keypress();
while ((c = getchar()) != EOF) {
if (c == 'q') break;
for (i = 0; i < 10 ; ++i) {
printf("%c", c);
}
printf("\n");
}
/* Restore terminal to previous mode */
reset_keypress();
return 0;
}
I have been asked to do it using ncurses and I don't want to learn ncurses from the scratch .
I am finding it really hard to understand ncurses ... I googled and found some sites which had info. but I amn't finding them quite good.
Can anyone plz suggest books/sites from which I can read abt ncurses
Maybe there are some functions in ncurses lib which will help me regarding taking character by character input.....
Actually what I want to do is implement "talk" in unix using C.
I have written "write" command using FIFOS [Though not as good as the one in unix....] Though maybe adding this thing and some other things would help...
The book "Beginning Linux Programming (2nd Edition)" has a chapter about ncurses. You can download the (GPL'd) sources for the examples in this book here:
I think it may be useful even without the book. Chapter 6 is about ncurses, and chapter 5 has a kbhit() (remember Borland on DOS?) program, similar to the example I posted.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.