Restore a stream ?

Hi, mates!
After reading a sream, for example :
string str;
getline(cin,str);
i need to restore it to its previous state, saving the information in str, cause i want to use the stream again as if hasnt beet read by now. I thought of converting str to char* and the using putback()/unget() functions to put back the char-s one by one, but it would look ugly.
Thank you in advance!
Last edited on
Even i tried doing so, my code crushes :(

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
string myName;
char* h = "12345";

getline(cin,myName);
cout<<endl<<myName<<endl;

char *buf = new char[myName.size()];

strcpy(buf,myName.c_str());
cout<<buf;

cin.ignore( numeric_limits<
streamsize>::max(), '\n' );

  for(int i=0 ; i<=4; i++)
  {
          cin.putback(buf[i]);
  }
 cout<<endl<<cin.rdstate();

 cin>>h;
 cout<<endl<<h;
http://www.cplusplus.com/reference/string/string/clear.html
If I understand you right, this you were looking for.
My question is, why do you wanna restore the stream? Why not cache the whole stream contents into a container so it can be reused?
That may be of help, but to use it in my case will nead a function/manipulator to move the seeker to next pos, but seekg() doesnt work properly or i dont use it correctly...
Zaita (1436) Nov 18, 2008 at 8:53pm
My question is, why do you wanna restore the stream? Why not cache the whole stream contents into a container so it can be reused?

Because im creating an input stream manipulater with one argument, that should try to read an object of type T (template), and if ot does but its not equal to the manipulator's argument, the manipulator should restore the initial state of the stream. (or if it is equal - to clrear the stream (whatever they mean by "clear "))
I'm not posting the hole task, cause i know people like you, Douas and else are not here to write our homeworks...but to guide us somehow.
Thank you!
Last edited on
:)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

using namespace std;

int main() {

  string word1;
  string word2;

  cout << "Enter word: ";
  cin >> word1;

  if (word1 != "world")
    for (int i = word1.length(); i > 0; --i)
      cin.putback(word1[i-1]);

  cin >> word2;
  cout << "World2 Was: " << word2;

  return 0;
}
Last edited on
That tells a lot! :)
10x :)
but unfortunately it crushes for getline(cin,word1) (i have to get all the sream)
can i make it work thatway ?
Last edited on
getLine strips the \n from the stream. >> doesn't. So if you use getline() you have to put the newline back into the stream too.

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
#include <iostream>
#include <string>

using namespace std;

int main() {

  string word1;
  string word2;

  cout << "Enter word: ";
  getline(cin, word1);

  if (word1 != "world") {
    cin.putback('\n');

    for (int i = word1.length(); i > 0; --i)
      cin.putback(word1[i-1]);
  }

  cin >> word2;
  cout << "World2 Was: " << word2;

  return 0;
}
Last edited on
Good to know!
Thank you again. Sorry if my question was stupid.
Last edited on
Topic archived. No new replies allowed.