What is a buffer?

I constantly read buffer for example flush it clears buffer or something. What is a BUFFER in C++ programming?

THANKS!!!

Just some block of memory. People tend to use the word "buffer" when talking about moving data around, because the data will be placed in the buffer, then placed in its final destination.

That's not always the case though, but it is one of those "feels right" programmer things. :-|

Here's an example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <algorithm>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
  {
  // This is where we'll put the stuff we read from file
  char buffer[ 81 ];

  // Here's our file
  ifstream file( "80-chars.txt" );

  // Fill the buffer with zeros (char strings must be null-terminated)
  fill_n( buffer, 81, '\0' );

  // Read as many as 80 bytes (chars) from the file and stick them in our array
  file.read( buffer, 80 );

  file.close();

  // Convert that char array into a STL string, and show the user what we got.
  string s( buffer );
  cout << s << endl;

  return 0;
  }


While this looks like the long way around to getting a string from file, it is actually pretty close to how getline() actually does it.

Hope this helps.
COOL! thanks so much :)
I have a question about the zeroing.
You use fill_n(pointer, size, '\0') to zero the memory of a variable.
I found from a Microsoft's example the ZeroMemory(pointer, size) command which do the same thing.
Which one you think is the best to use just to zero the variable?
Topic archived. No new replies allowed.