Find the answer to your Linux question:
Results 1 to 9 of 9
hey.. so i have this program that's supposed to printout a line whenever i type 'n'..i cant use scanf() function coz whenever i click it, it wont read the 'n' ...
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Posts
    60

    using scanf() without hitting return

    hey..
    so i have this program that's supposed to printout a line whenever i type 'n'..i cant use scanf() function coz whenever i click it, it wont read the 'n' except until i press return (Enter)...but this is not what i want.iwant it to display that line exactly after i press 'n', sort of like when u use the up and down arrows when u use the command: "less file.c"
    any help?

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    tcstattr() is your friend.

    Start by doing this at the command line:
    Code:
    man tcsetattr
    Then look at, compile, modify, and otherwise play with these tiny complete C programs which use tcsetattr().

    I had to upload them with extension .txt, because this site wouldn't let me upload them as .c. So you'll have to remove that before playing with them.

    Hope this helps.
    Attached Files Attached Files

  3. #3
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    I just use getch(). Might not be too portable though.

  4. #4
    Just Joined!
    Join Date
    Oct 2007
    Posts
    60
    Quote Originally Posted by likwid View Post
    I just use getch(). Might not be too portable though.
    how does it work?

  5. #5
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Actually, as part of the curses library, getch() is quite portable.

    So you'd be using the curses library. Although the calls are simpler than the programs I've included here, there can be complications if you don't wish all the other screen structuring capabilities that curses gives you.

    For more information, google this:
    Code:
    curses tutorial

  6. #6
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    Well, here's a useful example of a curses menu:

    Code:
    void menu(Person *list, char *fileName) {
       int key, menuSelection = 0;
       initscr();
    
       while (1) {
          draw(menuSelection);
          keypad(stdscr, TRUE);
          noecho();
    
          do {
             key = getch();
             switch(key) {
                case KEY_DOWN:
                   menuSelection++;
                   if (menuSelection > MENUITEMS - 1) menuSelection = 0;
                   break;
                case KEY_UP:
                   menuSelection--;
                   if (menuSelection < 0) menuSelection = MENUITEMS - 1;
                   break;
                default:
                   break;
             }
             draw(menuSelection);
          } while (key != '\n');
    }
    getch does what's called "Unbuffered Input" meaning that every character typed is read as it is input. It's extremely useful, like when you do "Press any key to continue". Notice that getch returns an int that is actually a char. The reason I say it is not portable though, is because AFAIK it is included in curses. Of course, it will work on most unix boxes, but probably not on Windows. wje's hints, albeit somewhat tedious methods, allude to the 'right' way to do it.

  7. #7
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    Quote Originally Posted by wje_lf View Post
    Although the calls are simpler than the programs I've included here, there can be complications if you don't wish all the other screen structuring capabilities that curses gives you.
    I think if you don't initialize the stdscr, you should be fine. But that is definitely true... I tried to make a curses frontend to this app once and it was kind of a nightmare because of the in and out buffers being messed with by curses.

  8. #8
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192

    portability to Microsoft(spit)(R) Windows

    Neither tcgetattr() nor the curses library is available for Windows, as far as I know.

  9. #9
    Linux Enthusiast likwid's Avatar
    Join Date
    Dec 2006
    Location
    MA
    Posts
    649
    O I thought tcgetattr was part of the C standard, but it looks like it's in unistd.h, so that's a no for win compatibility. IDK, some people program on Windows, not me lol.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...