Implementing a custom stream input/output manipulator

In this blog post, we will explore how to implement a custom stream input/output (I/O) manipulator in C++. Stream I/O manipulators are functions that allow us to control the formatting of input and output operations on streams.

Understanding Stream I/O Manipulators

Stream I/O manipulators are functions that can be used with input and output streams in C++. They provide a convenient way to control the behavior of input and output operations on streams. Examples of built-in manipulators include setw, setprecision, and fixed.

Implementing a Custom Manipulator

To implement a custom manipulator, we need to define a function that takes a stream as an argument and returns a modified stream. The function should modify the stream’s state as desired and return the updated stream. Let’s consider an example where we want to convert all characters in a stream to uppercase.

#include <iostream>
#include <cctype>

std::ostream& uppercase(std::ostream& stream) {
  char c;
  while (stream.get(c)) {
    stream.put(std::toupper(c));
  }
  return stream;
}

int main() {
  std::cout << "Hello, World!" << std::endl;
  std::cout << uppercase << "Hello, World!" << std::endl;
  return 0;
}

In the code above, we define a function named uppercase that takes an output stream as an argument. Inside the function, we use get to read characters from the stream one by one. For each character, we convert it to uppercase using std::toupper and then write it back to the stream using put. Finally, we return the modified stream.

In the main function, we demonstrate how to use our custom manipulator. First, we output the string “Hello, World!” as-is. Then we use the uppercase manipulator by writing uppercase before the string. This causes all characters in the string to be converted to uppercase before being printed.

Conclusion

In this blog post, we learned how to implement a custom stream I/O manipulator in C++. We saw how to define a function that modifies the stream’s state and returns the updated stream. By implementing custom manipulators, we can have fine-grained control over the formatting of our input and output operations.

#C++ #StreamManipulator