i was reading xerces-c class documentation, one of the methods does the writing of buffer to a file. what should i understand from buffer, how is it different from writing some stream to a file?
What should i understand from stream?

Thanks

Recommended Answers

All 5 Replies

According to the definitions, buffer is the temporary storage area whereas a stream is a sequence of data of undetermined length.
So if there is a stream, since its size is not defined, you put it in a defined section in your temporary memory and work with it. So the method is named to save buffer in the memory because its size was determined. That is what i infered from what i read, if you want to improve my inference, please go for it.

A buffer is normally just a block of memory where things can be stored in RAM. A stream is something that lets you store things on disk, send across to other computers such as the internet, serial port, UCB, etc. streams often use buffers to optimize transmission speed. For example instead of writing data directly to disk the stream (e.g. ofstream object) might store it in a buffer and write to disk when either the buffer is full or when there is available time such as while the program is waiting for keyboard input.

I'm assuming you mean a buffer in the context of a stream.

A stream is a sequential list of items where (in the simplest sense), the source places a single item at a time onto the stream and the destination extracts a single item in the order of insertion. In other words, a stream is one concrete implementation of a FIFO queue.

A buffered stream is a stream with a buffer (obviously!), but what's the point? Consider something like an input file stream, where physically moving to the correct record on your hard drive and loading data into memory is relatively slow. If you have a character stream and each request for a character has to go back to the hardware, you're going to see some serious latency.

The solution is to read as much as possible when you go back to the hardware and store that data in a temporary location that's quicker to access. This is the buffer. Then when a character is requested, if the buffer isn't empty then it will satisfy the request instead of going to the hardware. The only time you need to go back to the hardware is to refill the buffer when it becomes empty.

The end result is that the input file stream is drastically faster because instead of (for example) one million hardware accesses to read one million characters, a buffer of ten thousand drops the number of hardware accesses to one hundred. Most of the requests are satisfied by taking a character from memory and incrementing an index.

Here's a quick example:

#include <cstdio>
#include <iostream>

class buffered_stream {
public:
  static const int BUFFER_SIZE = 5;
public:
  buffered_stream(): i ( 0 ), n ( 0 ) {}
public:
  bool get ( char& ch )
  {
    if ( i == n ) {
      std::cout<<"Buffer empty, refilling\n";
      i = 0;
      n = std::fread ( buffer, sizeof *buffer, BUFFER_SIZE, stdin );
    }

    if ( n != 0 ) {
      std::cout<<"Reading from buffer\n";
      ch = buffer[i++];
    }

    return n != 0;
  }
private:
  char buffer[BUFFER_SIZE];
  std::size_t i;
  std::size_t n;
};

int main()
{
  buffered_stream in;
  char ch;

  while ( in.get ( ch ) )
    std::cout<< ch <<'\n';
}

In the end, buffering is a form of caching, and it's there only to improve performance.

I admire you Narue, thanks a lot.

Agree with Narue.
Very clear points

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.