Unbuffered character input

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jules

    Unbuffered character input

    I am trying to understand the behavior of a short program. Hopefully
    someone can tell me what's going on.

    The program I wrote is:
    #include <stdio.h>

    main()
    {
    char c;
    while(1)
    {
    c=getchar();
    printf("The character pressed was %c\n",c);
    if(c=='q')
    break;
    }
    }


    I run it, and start pressing keys, and nothing happens. When I hit
    return, I see a series of lines on the screen, one for each character
    I pressed. If one of those keys is 'q,' the program does not exit
    until I press return. I guess this means the input is being
    buffered. Seems simple enough.
    Next, I tried running the same program, piping the output to less.
    Now, I don't see any of the output until the program exit, at which
    point less displays the entire output. The funny thing here is that
    the input is not buffered: As soon as I press q, the program
    terminates and all of the output is displayed by less.
    Can someone explain to me why the input is buffered in the first case
    but not in the second? Also, if I want to get non-buffered input from
    the keyboard, is there an easy way to do this? On my old computer, I
    would use getch(), but I have learned that this is not standard, and
    is not supported by the compiler I am using now.
  • Malcolm McLean

    #2
    Re: Unbuffered character input


    "Jules" <julianrosen@gm ail.comwrote in message
    Also, if I want to get non-buffered input from
    the keyboard, is there an easy way to do this? On my old computer, I
    would use getch(), but I have learned that this is not standard, and
    is not supported by the compiler I am using now.
    >
    There is no standard method. As you have found out, the stdio functions are
    line-based.
    However most platforms provide a function called kbhit() or an equivalent.
    Beware that you might have to call functions to set up the console and close
    it again.

    --
    Free games and programming goodies.



    Comment

    • Spiros Bousbouras

      #3
      Re: Unbuffered character input

      On 18 May, 23:45, Jules <julianro...@gm ail.comwrote:
      I am trying to understand the behavior of a short program. Hopefully
      someone can tell me what's going on.
      >
      The program I wrote is:
      #include <stdio.h>
      >
      main()
      {
      char c;
      while(1)
      {
      c=getchar();
      printf("The character pressed was %c\n",c);
      if(c=='q')
      break;
      >
      }
      }
      >
      I run it, and start pressing keys, and nothing happens. When I hit
      return, I see a series of lines on the screen, one for each character
      I pressed. If one of those keys is 'q,' the program does not exit
      until I press return. I guess this means the input is being
      buffered. Seems simple enough.
      Next, I tried running the same program, piping the output to less.
      Now, I don't see any of the output until the program exit, at which
      point less displays the entire output. The funny thing here is that
      the input is not buffered: As soon as I press q, the program
      terminates and all of the output is displayed by less.
      Can someone explain to me why the input is buffered in the first case
      but not in the second?
      I presume you're running your programme on a
      Unix (like) operating system. The answer to your
      question has nothing to do with standard C which
      is what we discuss here.

      out_of_topic {
      I presume the input becomes unbuffered
      because less arranges things these way.
      You will notice that if you do
      a.out | cat
      the input is again buffered.
      }
      Also, if I want to get non-buffered input from
      the keyboard, is there an easy way to do this? On my old computer, I
      would use getch(), but I have learned that this is not standard, and
      is not supported by the compiler I am using now.
      This is question 12.5 in the FAQ.


      For Unix specific information you can ask
      at comp.unix.progr ammer

      Comment

      • Spiros Bousbouras

        #4
        Re: Unbuffered character input

        On 19 May, 00:40, Spiros Bousbouras <spi...@gmail.c omwrote:
        On 18 May, 23:45, Jules <julianro...@gm ail.comwrote:
        >
        >
        >
        I am trying to understand the behavior of a short program. Hopefully
        someone can tell me what's going on.
        >
        The program I wrote is:
        #include <stdio.h>
        >
        main()
        {
        char c;
        while(1)
        {
        c=getchar();
        printf("The character pressed was %c\n",c);
        if(c=='q')
        break;
        >
        }
        }
        >
        I run it, and start pressing keys, and nothing happens. When I hit
        return, I see a series of lines on the screen, one for each character
        I pressed. If one of those keys is 'q,' the program does not exit
        until I press return. I guess this means the input is being
        buffered. Seems simple enough.
        Next, I tried running the same program, piping the output to less.
        Now, I don't see any of the output until the program exit, at which
        point less displays the entire output. The funny thing here is that
        the input is not buffered: As soon as I press q, the program
        terminates and all of the output is displayed by less.
        Can someone explain to me why the input is buffered in the first case
        but not in the second?
        >
        I presume you're running your programme on a
        Unix (like) operating system. The answer to your
        question has nothing to do with standard C which
        is what we discuss here.
        >
        out_of_topic {
        I presume the input becomes unbuffered
        because less arranges things these way.
        ^^^^
        Eeeek.
        You will notice that if you do
        a.out | cat
        the input is again buffered.
        >
        }
        Also, if I want to get non-buffered input from
        the keyboard, is there an easy way to do this? On my old computer, I
        would use getch(), but I have learned that this is not standard, and
        is not supported by the compiler I am using now.
        >
        This is question 12.5 in the FAQ.http://c-faq.com/
        >
        For Unix specific information you can ask
        at comp.unix.progr ammer

        Comment

        • Flash Gordon

          #5
          Re: Unbuffered character input

          Malcolm McLean wrote, On 19/05/08 00:06:
          >
          "Jules" <julianrosen@gm ail.comwrote in message
          >Also, if I want to get non-buffered input from
          >the keyboard, is there an easy way to do this? On my old computer, I
          >would use getch(), but I have learned that this is not standard, and
          >is not supported by the compiler I am using now.
          >>
          There is no standard method. As you have found out, the stdio functions
          are line-based.
          However most platforms provide a function called kbhit() or an
          equivalent. Beware that you might have to call functions to set up the
          console and close it again.
          If the OP is using a Unix like system (and use of less suggests but does
          not guarantee this) then it almost certainly has a different name and
          operates differently. comp.unix.progr ammer would probably be a
          reasonable place for the OP to ask after checking their FAQ.
          --
          Flash Gordon

          Comment

          Working...