Is it possible to make a copy of cin?

I have a function that now gives me the size of the cin.buffer, but it empties cin in the process. Could I make a copy of cin such as like this:

istream copyofcin(cin);
and then get the size of copyofcin?

not as far as I know... but you should be able to get the size of the data in cin without clearing it:

 
int data_in_cin = cin.rdbuf()->in_avail();


EDIT: note I didn't actually try that code -- it just looks like it should do the job based on the docs)
Last edited on
It is ridiculously difficult to make a copy of a stream object, partially because all the copy constructors and assignment operators are protected. O_o

I would suggest following Disch's advice: try copying all the data in cin without clearing it.

-Albatross

EDIT: 100 off 2000 posts, and counting.
Last edited on
Thanks that looks great. But is that code non standard by any chance? I tried running the following code from msdn.microsoft.com

1
2
3
4
5
6
7
8
9
10
11
12
13
// basic_streambuf_in_avail.cpp
// compile with: /EHsc
#include <iostream>

int main( ) 
{
   using namespace std;
   char c;
   // cin's buffer is empty, in_avail will return 0
   cout << cin.rdbuf( )->in_avail( ) << endl;
   cin >> c;
   cout << cin.rdbuf( )->in_avail( ) << endl;
}


and ran it with identical input, namely
4
but instead of getting output:

0
4
1


I got output:

0
4
0


Am I cursed?
Last edited on
Quite. in_avail() is non-portable. It is part of the C++ standard, however the standard does not require that it returns what one would expect or even anything useful at all.
I found elsewhere on the net that if I have
std::ios::sync_with_stdio(false); it would work, and indeed now it does. but what does
std::ios::sync_with_stdio(false); do?
Problem solved. I think.

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
#include <iostream>
#include <string>
//#include <windows.h>
using namespace std;

bool turn_off_sync(){std::ios::sync_with_stdio(false); return true;}

bool syncstatus = !turn_off_sync();

streamsize cinsize(istream & is)
{
    streamsize data_in_cin = is.rdbuf()->in_avail();
    return data_in_cin;
}

bool clearcin(istream& is)
{
    bool rv = false;
    ios_base::iostate ioinfo = is.rdstate();
    is.clear();
    streamsize s = cinsize(cin);
    if (s >= 1)
        rv = true;
    do
        {
            is.clear();
            is.ignore(s, '\n');
            s = cinsize(is);
        }while(s >= 1);
    is.clear(ioinfo);
    return rv;
}

int main()
{
    cout << "This is a test.\n";
    //clearcin(cin); exit(2);
    int x;
    while(1)
        {
            cout << "Enter something.\n";
            cin >> x;
            cout << "You entered: " << x << endl;
            cout << "cinsize(cin) = " << cinsize(cin) << "!" << endl;
            if (cin.fail())
                {
                    clearcin(cin);
                    cin.clear(cin.rdstate() &~ ios::failbit);
                }
        }
    return 0;
}
Topic archived. No new replies allowed.