Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
typo
Source Link
user1356190
  • 79
  • 1
  • 2
  • 5

[SOLUTION]

You can pass a stringstream into the paramater that takes an ostream. After that you can get the text that was set inside the stringstream by its str() funktion.

#include <sstream> ... { stringstream ss; print(ss); string text; text = ss.str(); // Handle text } 

Given there's a function like this:

void print(ostream& stream) const; 

How can I access the data that is written into the ostream?

I was thinking of interpreting it as a string:

string text; print( text ); // Interpret text 

The reason is there's another function that has a pointer to an object as a parameter:

void append( *Data data ); 

And I must use the data objects print method to get at its data:

// This is a public function in the Data class void append( *Data data ) { string text; data->print( text ); // append data to this Data object } 

I was trying to cast to a stringstream but that didn't work. I'm thinking if I could save the data to a file and then read it as workaround.

Given there's a function like this:

void print(ostream& stream) const; 

How can I access the data that is written into the ostream?

I was thinking of interpreting it as a string:

string text; print( text ); // Interpret text 

The reason is there's another function that has a pointer to an object as a parameter:

void append( *Data data ); 

And I must use the data objects print method to get at its data:

// This is a public function in the Data class void append( *Data data ) { string text; data->print( text ); // append data to this Data object } 

I was trying to cast to a stringstream but that didn't work. I'm thinking if I could save the data to a file and then read it as workaround.

[SOLUTION]

You can pass a stringstream into the paramater that takes an ostream. After that you can get the text that was set inside the stringstream by its str() funktion.

#include <sstream> ... { stringstream ss; print(ss); string text; text = ss.str(); // Handle text } 

Given there's a function like this:

void print(ostream& stream) const; 

How can I access the data that is written into the ostream?

I was thinking of interpreting it as a string:

string text; print( text ); // Interpret text 

The reason is there's another function that has a pointer to an object as a parameter:

void append( *Data data ); 

And I must use the data objects print method to get at its data:

// This is a public function in the Data class void append( *Data data ) { string text; data->print( text ); // append data to this Data object } 

I was trying to cast to a stringstream but that didn't work. I'm thinking if I could save the data to a file and then read it as workaround.

Source Link
user1356190
  • 79
  • 1
  • 2
  • 5

How to read output from ostream?

Given there's a function like this:

void print(ostream& stream) const; 

How can I access the data that is written into the ostream?

I was thinking of interpreting it as a string:

string text; print( text ); // Interpret text 

The reason is there's another function that has a pointer to an object as a parameter:

void append( *Data data ); 

And I must use the data objects print method to get at its data:

// This is a public function in the Data class void append( *Data data ) { string text; data->print( text ); // append data to this Data object } 

I was trying to cast to a stringstream but that didn't work. I'm thinking if I could save the data to a file and then read it as workaround.