Search code examples
c++unistd.hstreambuf

Where can I find a reference to the function write?


I have the following code for the definition of a streambuf class. I have to adapt it to my needs. Before that, I have to understand how the code actually works. Can anyone tell me where I can find a reference to the write function in flushBuffer. It takes 3 parameters and returns an int. std::streambuf does not have such a member...

//code taken from: The C++ Standard Library Second Edition, Nicolai M. Josuttis, p. 837

class Outbuf_buffered_orig : public std::streambuf {
    protected:
        static const int bufferSize = 10; // size of data buffer
        char buffer[bufferSize]; // data buffer
    public:
        // constructor
        // - initialize data buffer
        // - one character less to let the bufferSizeth character cause a call of overflow()
        Outbuf_buffered_orig() {
        setp (buffer, buffer+(bufferSize-1));
        }
        
        // destructor
        // - flush data buffer
        virtual ~Outbuf_buffered_orig() {
        sync();
        }

    protected:
        // flush the characters in the buffer
        int flushBuffer () {
        int num = pptr()-pbase();
        if (write (1, buffer, num) != num) {
        return EOF;
        }
        pbump (-num); // reset put pointer accordingly
        return num;
        }
        
        // buffer full
        // - write c and all previous characters
        virtual int_type overflow (int_type c) {
        if (c != EOF) {
        // insert character into the buffer
        *pptr() = c;
        pbump(1);
        }
        // flush the buffer
        if (flushBuffer() == EOF) {
        // ERROR
        return EOF;
        }
        return c;
        }

        // synchronize data with file/destination
        // - flush the data in the buffer
        virtual int sync () {
        if (flushBuffer() == EOF) {
        // ERROR
        return -1;
        }
        return 0;
        }
};  //Outbuf_buffered

Solution

  • Can anyone tell me where I can find a reference to the write function

    This is Linux' ssize_t write(int fd, const void *buf, size_t count), defined in <unistd.h>.

    See man 2 write for more information.

    Note: write(1, ...) writes to file descriptor #1: standard output.