Thread: A version of getchar that does unbuffered input, K&R, chapter 8, read()

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    54

    A version of getchar that does unbuffered input, K&R, chapter 8, read()

    Hi!

    Do I understand the following code correctly?

    Code:
    int getchar(void)
    {
        static char buf[BUFSIZ];
        static char *bufp = buf;
        static int n = 0;
        
        if (n == 0)
        { /* buffer is empty */
            n = read(O, buf, sizeof buf);
            bufp = buf;
        }
        return (--n >= 0) ? (unsigned char) *bufp++ : EOF;
    }
    If character array buf is empty (if (n == 0), the function read()
    will read a character string of a size sizeof(buf) which is equal to
    the size of declared array buf. So, this array buf after read () is done
    will contain a few characters.

    I am not sure I understand the return statement:
    Code:
     return (--n >= 0) ? (unsigned char) *bufp++ : EOF;
    If after decrementing n the value of n is bigger or equal to 0, then
    the program returns the first character of the array buf (buf[0]) to
    which bufp points to. Then bufp is incremented to point to the next
    character. How does the whole inputted array gets printed? It seems
    that only one character is printed.
    (If error, the program returns EOF; it's clear).
    Thanks for your help!

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    The idea is that read'ing is an expensive operation. Therefore, when you do a read, you may as well get several characters - that's the purpose of buf. BUFSIZ is a constant defined to be a well-chosen value for reading several characters at one time. For example, read'ing 1-2 characters at a time is probably too few, while reading 10 million at a time is probably too many.

    Quote Originally Posted by Ducol View Post

    How does the whole inputted array gets printed? It seems
    that only one character is printed.
    The user of your getchar function will only get one character at a time. Basically, the user doesn't have to care that there is buffering going on behind the scenes. You use this function just like you did back in Chapter 1.

    Quote Originally Posted by Ducol View Post
    I am not sure I understand the return statement:
    Code:
     return (--n >= 0) ? (unsigned char) *bufp++ : EOF;
    It may help to read this type of statement slowly, and break it into multiple statements. It basically works out to be the same as this:

    Code:
    --n;
    if (n >= 0) {
        int ret = (unsigned char) *bufp;
        bufp++;
        return ret;
    } else
        return EOF;

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    54
    Quote Originally Posted by c99tutorial View Post
    The idea is that read'ing is an expensive operation. Therefore, when you do a read, you may as well get several characters - that's the purpose of buf. BUFSIZ is a constant defined to be a well-chosen value for reading several characters at one time. For example, read'ing 1-2 characters at a time is probably too few, while reading 10 million at a time is probably too many.
    Thanks a lot for your answer. I understand that. I also understand what the last return says, it's quite a clear statement. Maybe I have not clearly
    asked my question, but you have confirmed my statement that there will be only one character printed at one call of this function.
    But read() will receive a whole array of characters. If only one character gets printed, what happens to the rest of characters, and how does the program access all other characters to get the whole line printed?

    For example, the following code will print the whole array:
    Code:
    int main(void) /*copy input to output*/
    {
        char buf[BUFSIZE];
        int n;
        
        while ((n = read(0, buf, BUFSIZE)) > 0)
        {
            write(1, buf, n);
        }
        return 0;
    }
    It has a loop, and thus all character received by read() will be printed.
    Last edited by Ducol; 12-03-2015 at 03:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New program to read three numbered integer using getchar
    By Interista in forum C Programming
    Replies: 3
    Last Post: 11-03-2011, 01:45 PM
  2. read in from serial port; getchar()
    By s.dodd in forum C Programming
    Replies: 9
    Last Post: 10-23-2011, 10:50 AM
  3. unbuffered input, how to do it?
    By elninio in forum C++ Programming
    Replies: 4
    Last Post: 07-11-2008, 04:33 PM
  4. How do I input an unbuffered character via the keyboard???
    By Michael_in_Ohio in forum C Programming
    Replies: 1
    Last Post: 03-23-2008, 12:00 PM
  5. Single unbuffered input in C++
    By ronin in forum C++ Programming
    Replies: 8
    Last Post: 02-21-2003, 07:59 PM

Tags for this Thread