Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I use the >> and << operators for binary data in C++?

Tags:

c++

stream

Is there a way to use these operators to input and output binary data? The reason I want to do this is that it makes the code readable. Ex: infile >> filedecrypter >> metadataparser >> audiodecoder >> effects >> soundplayer;

like image 958
Carl Avatar asked Jan 25 '23 00:01

Carl


1 Answers

Just to be clear, are you intending to duplicate the semantics of iostreams? Because it looks like you are proposing something different. In the example you give:

infile >> filedecrypter >> metadataparser >> audiodecoder >> effects >> soundplayer;

In iostreams, the meaning here is to read from infile into filedecrypter until you get to whitespace, and then from infile into metadataparser until more whitespace, and so on.

It looks like you are proposing something different, where metadataparser reads from filedecrypter, audiodecoder from metadataparser, etc. In which case I think the answer to your question needs to be qualified a bit.

Can you use operator >> to express this construct? Probably yes.

Can you use iostreams for this? Probably not.

I suggest you clarify what it means when you say A >> B. Perhaps express it as regular methods rather than operator overloads first, and that may clarify the question.

like image 61
Alastair Avatar answered Jan 26 '23 12:01

Alastair