Is there a way to see what's in the buffer?

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:
1
2
3
4
5
6
int array[10];
fill_array(array, 10);
unsigned char *pointer_to_buffer = (unsigned char *)array;
unsigned char *another_pointer_to_buffer = compress(pointer_to_buffer, sizeof(array));
do_something_or_other(another_pointer_to_buffer);
delete[] another_pointer_to_buffer;

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.
Last edited on
> allow a programmer to see what is in the buffer at any given time?
> so that when you ran tests you could see what was in it at any given point.

The simplest way (non-destructive; without modifying the input buffer) is to examine the input buffer in a debugger.

You appear to be using Visual Studio, here is a walk through:

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
29
30
31
32
33
34
35
36
37
38
39
#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) ;

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <string>
#include <iomanip>
#include <cctype>

std::string peek_buffer( std::istream& stm )
{
    struct transparent_buffer : std::streambuf
    {
        static std::string view( std::streambuf* buf )
        {
            if( buf == nullptr ) return {} ;

            const auto tbuf = static_cast< transparent_buffer* >(buf) ;
            return { tbuf->gptr(), tbuf->egptr() } ;
        }
    };

    return transparent_buffer::view( stm.rdbuf() ) ;
}

void see_buffer( std::string desc = {}, std::istream& stm = std::cin )
{
    const std::string str = peek_buffer(stm) ;

    std::string escaped_str ;
    for( char c : str )
    {
        if( c == '\n' ) escaped_str += "\\n" ;
        else if( c == '\t' ) escaped_str += "\\t" ;
        else escaped_str += c ;
    }

    std::cout << desc << " -  buffer size: " << str.size() << "  contents: '" << escaped_str << "'\n" ;
    if( stm.fail() ) std::cout << "(note: stream is in a failed state)\n" ;
    std::cout << '\n' ;
}

int main()
{
    // std::cin.sync_with_stdio(false) ;

    see_buffer( "initially" ) ;
    int v ;
    while( std::cin >> v ) see_buffer( "after extracting " + std::to_string(v) ) ;

    see_buffer( "after input failed" ) ;

    std::cin.clear() ;
    see_buffer( "after clear" ) ;

    std::string str ;
    std::cin >> str ;
    see_buffer( "after extracting '" + str + "'" ) ;

    std::cin >> v ;
    see_buffer( "after extracting " + std::to_string(v) ) ;

    std::cin.ignore( 1000, '\n' ) ;
    see_buffer( "after ignore" ) ;
}

Input: 123 456 abcd 789 xyz
Output:
initially -  buffer size: 0  contents: ''

after extracting 123 -  buffer size: 18  contents: ' 456 abcd 789 xyz\n'

after extracting 456 -  buffer size: 14  contents: ' abcd 789 xyz\n'

after input failed -  buffer size: 13  contents: 'abcd 789 xyz\n'
(note: stream is in a failed state)

after clear -  buffer size: 13  contents: 'abcd 789 xyz\n'

after extracting 'abcd' -  buffer size: 9  contents: ' 789 xyz\n'

after extracting 789 -  buffer size: 5  contents: ' xyz\n'

after ignore -  buffer size: 0  contents: ''

Microsoft: http://rextester.com/IWB53473
libstdc++: http://rextester.com/DOBEC55222
libc++ (broken): http://rextester.com/YZX82414
Also of interest might be std::istream::peek and particulary its associated function sgetc that ...
(r)eturns the character at the current position of the controlled input sequence, without modifying the current position.
(emphasis added) http://www.cplusplus.com/reference/streambuf/streambuf/sgetc/
Topic archived. No new replies allowed.