How can I get my own stream from a standard stream?

How can I get my own stream from a standard stream?

There is a Stream class in C #, but C ++ streams are too complex.

I need something like this:

class my_stream : public std::stream { // How to derive? }; void using_a_stream(std::stream* s) { *s << "Hello world"; } void main() { std::stream s1; std::fstream s2("C:\\test.txt"); my_stream s3; using_a_stream(&s1); using_a_stream(&s2); using_a_stream(&s3); } 

Note. The code is just a sample and may be an invalid C ++ program. Thanks.

+6
source share
3 answers

I think there are three levels of answer to this question:

Level 1 This is difficult, especially if you are completely new to C ++, stop right now. Only if you feel adventurous, continue on level 2.

Level 2 Use a library that simplifies thread creation. I would suggest using the Boost.IOStreams library. This makes it easy to create your own streams and streambuf. If you are still not satisfied, continue level 3.

Level 3 : you will need to get std::streambuf and change its behavior according to your needs. Then you need to connect your streambuf to your own stream .

+15
source

Could you describe a little more than what you have to do to create a streamclass class? Just asking how without what not the best way to get a constructive answer.

Perhaps you should take a look at boost::iostream , as there is a much simpler and safer way to write your own iostream classes.

0
source

Source: https://habr.com/ru/post/891431/


All Articles