Simple question. Is there any function available to allow a programmer to see what is in the buffer at any given time? Something like seebuffer(); something or other. It seems like the buffer is a mysterious invisible concept where things are stored and no one really knows what's in there at any given time, you just kind of have to follow it in your head as you create your program so that you won't allow any errors to occur because of parts of the program using data left over in the buffer. It'd be great if there was a function that you could implement spaced out over your program so that when you ran tests you could see what was in it at any given point.
The buffer? I don't know what that is. A buffer, on the other hand, I do know. A buffer is just a piece of memory that's treated by the program as a simple array of bytes, without any more complex reinterpretation. For example:
Visualizing or verifying data in a buffer is typically difficult because a program may contain a lot of data in memory at once. Some programs may hold gigabytes at once, in various buffers.
#include <iostream>
int main()
{
std::cout << "type in a few integers separated by white space\n" ;
// enter 123 456 abcd 789 <new line>int i ;
std::cin >> i ; // extract the first int
auto input_buffer = std::cin.rdbuf() ;
// after the previous line was executed, examine input_buffer in the debugger
// the following assumes that we are using the integrated debugger in the current version of
// Visual Studio (with either clang++ or microsoft c++ as the front end)
// look at the variable input_buffer in the locals window of the debugger
// For the microsoft library, these are the members of interest
// input_buffer->_IGcount == 15 (number of characters remaing to be read in the input buffer)
// input_buffer->_IGnext == " 456 abcd 789 \n" (the 15 characters remaing to be read)
std::cin >> i ; // extract the next int
// after the previous line was executed, examine input_buffer in the debugger
// input_buffer->_IGcount == 11 (number of characters remaing to be read in the input buffer)
// input_buffer->_IGnext == " abcd 789 \n" (the 11 characters remaing to be read)
std::cin.ignore( 1000, '\n' ) ;
// after the previous line was executed, examine input_buffer in the debugger
// input_buffer->_IGcount == 0 (number of characters remaing to be read in the input buffer)
// there is nothing left to be read in the input buffer
std::cin >> i ; // extract another int: gcount == 0, so waits for user to enter something
// enter 789 hello world!<new line>
// after the previous line was executed, examine input_buffer in the debugger
// input_buffer->_IGcount == 14 (number of characters remaing to be read in the input buffer)
// input_buffer->_IGnext == " hello world!\n" (the 14 characters remaing to be read)
}
> Is there any function available to allow a programmer to see what is in the buffer at any given time?
> Something like seebuffer()
Hmm... Such a function could be quite useful, particularly for those who have just started learning about the i/o facilities offered by the standard library.
Here's is an off-the-cuff version of see_buffer()
It appears to work correctly with clang++, microsoft C++ and GNU g++/libstdc++ on windows
and with GNU g++/libstdc++ on linux.
It is broken on LLVM clang++/libc++ on linux
Note: GNU g++/libstdc++ requires a call to sync_with_stdio(false) ;