String Streams

Resetting a String Stream

How do I reset the content of a string stream?

Abstract art representing computer programming

Resetting a string stream in C++ is straightforward. You can use the str() method to set new content for the stream, which also resets the stream position to the beginning.

This method takes a std::string as an argument and replaces the current content with the new string. Here’s an example:

#include <iostream>
#include <sstream>

int main() {
  std::ostringstream Stream;
  Stream << "Initial content";
  std::cout << "Stream: "
    << Stream.str() << "\n";

  Stream.str("New content");  
  std::cout << "Stream: "
    << Stream.str() << "\n";
}
Stream: Initial content
Stream: New content

When you call Stream.str("New content"), it sets the stream's content to "New content" and moves the output position to the beginning.

Clearing the Stream

If you want to clear the stream without setting new content, you can pass an empty string to the str() method:

Stream.str("");

Alternatively, you can clear the stream state using the clear() method, which resets the stream's error state flags:

Stream.clear();

However, clear() does not remove the content of the stream. It only resets the state flags. To completely reset the stream (content and state), you should use both methods:

Stream.str("");
Stream.clear();

This combination ensures that the stream is empty and ready for new operations.

Practical Example

Here’s a practical example where you might want to reset a string stream:

#include <iostream>
#include <sstream>

void ProcessData(const std::string& data) {
  std::ostringstream Stream;
  Stream << data;
  std::cout << "Processed: "
    << Stream.str() << "\n";

  Stream.str("");  
  Stream.clear();

  // New data processing
  Stream << "Additional data";
  std::cout << "Processed: "
    << Stream.str() << "\n";
}

int main() {
  ProcessData("First data");
}
Processed: First data
Processed: Additional data

In this example, we reset the stream before processing new data to ensure the old data does not interfere with new operations.

This Question is from the Lesson:

String Streams

A detailed guide to C++ String Streams using std::stringstream. Covers basic use cases, stream position seeking, and open modes.

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

String Streams

A detailed guide to C++ String Streams using std::stringstream. Covers basic use cases, stream position seeking, and open modes.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved