Read keyb in uninterrupted loop

This is a discussion on Read keyb in uninterrupted loop within the C Programming forums, part of the General Programming Boards category; Hello I'm looking for a way to input text from the keyboard - whithout having to wait for the user, ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    5

    Question Read keyb in uninterrupted loop

    Hello

    I'm looking for a way to input text from the keyboard - whithout having to wait for the user, because I have to do some other checks in the loop.

    I've tried with kbhit but that results in the user are forced to perform an extra keypress after she has pressed enter.

    But as I understand is using getchar() results in having the loop waiting for input - thus delaying the loop heavily - which means that I have to sniff a bit in the keyb buffer before reading the characters entered by the user. There for I have to some precheck or ????

    Code:
    while(1)
    {
      if (kbhit()!=0)
       {
         if ((c=getchar()) != '\n')
       {
         myString[i]=(char)c;
         i=i+1;
       }
        else
              {
                //Data processing
              }
       }
         //SEND
         //RECEIVE
         // Delay if needed
    }
    Heelp somebody
    I would really appreciate it.

    Many regards
    Peter
    Last edited by flywheel; 01-15-2009 at 04:39 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What OS/Compiler are you using?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    5
    Hi there

    According to the help files it is an GNU CC version 2.7.2 - modified for embedded purposes by HighTec EDV-Systemes.

    Many regards

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, so you are probably not using a standard C library either? That makes the whole business a whole lot harder.

    Could you use "getch()" instead of getchar()?

    [Trivia question: What year was gcc 2.7.2 produced?]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    5
    No unfortunately conio.h is not available

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by matsp View Post
    [Trivia question: What year was gcc 2.7.2 produced?]
    Guess: 1993

    That must have been a good one tho, for years you could only compile the linux kernel with 2.7.2
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by flywheel View Post
    No unfortunately conio.h is not available
    But you have a "kbhit()", so look at what "kbhit()", or look a the low-level bits of code dealing with getchar() [but that may be quite hard to find, as there is probably quite a few levels between the call to getchar() and the actual reading of the data], and write a getch() function.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The other obvious solution would be to use two threads.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    5
    Threads is also a problem - perhaps I should look into moving this part into a PC-client.
    Which means that I would have conio.h at my displosal.

    What could I do if I had getch ?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by flywheel View Post
    Threads is also a problem - perhaps I should look into moving this part into a PC-client.
    Which means that I would have conio.h at my displosal.

    What could I do if I had getch ?
    getch() by tradition waits for a single character to be ready, and then goes on. Since you already have kbhit, you can determine when to call getch() without having to wait at all.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    5
    Quote Originally Posted by matsp View Post
    getch() by tradition waits for a single character to be ready, and then goes on. Since you already have kbhit, you can determine when to call getch() without having to wait at all.
    But doesn't getchar do the same as getch - it just echoes the character to stdin ?

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by flywheel View Post
    But doesn't getchar do the same as getch - it just echoes the character to stdin ?
    getchar does not process input till the enter is pressed
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 11:21 PM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21