2

If this snippet of code saves an an object/image to file. What would need to be modified for it to actually cout the image's binary data stream? Thanks!

if (frame && frame->Contains(ID3FN_DATA)) { cout << "*** extracting picture to file \"" << argv[2] << "\"..."; frame->Field(ID3FN_DATA).ToFile(argv[2]); cout << "done" << endl; } 
3
  • 2
    What library are you using? The answer will be specific to that library, since this isn't part of the C++ spec. Commented Feb 11, 2011 at 3:37
  • @templatetypedef, It's from ID3lib and I just want to instead of this: frame->Field(ID3FN_DATA).ToFile(argv[2]); send frame->Field(ID3FN_DATA) to cout Commented Feb 11, 2011 at 4:01
  • 2
    You can't, at least not portably: std::cout is a text stream and there is no portable way to reopen it as a binary stream. What, exactly, are you trying to do? It usually doesn't make a whole lot of sense to dump a bunch of binary data out to the console. Commented Feb 11, 2011 at 4:08

1 Answer 1

3

I'm not familiar with this library particularly, but the documentation on the website suggests that that once you have the ID3_Field object, you can get its raw binary size using ID3_Field::BinSize() and the raw bytes from ID3_Field::GetRawBinary(). Once you have these two parameters, you can write the binary data to any ostream, including cout, by calling

myOStream.write((char *)field->GetRawBinary(), field->BinSize()); 

I have no idea if this is going to work because I've never used this library, but at least intuitively this makes sense.

Hope this helps!

Sign up to request clarification or add additional context in comments.

3 Comments

It is my understanding that this won't work: because std::cout is a text stream, std::ostream::write will perform newline translation.
@James McNellis- What if the output was to a stream other than cout? Would it work then? And if that won't work, is there any hope for doing this in a cross-platform way?
It will work if it's a binary stream, but as I noted in a comment to the original post, there's no portable way to reopen std::cout as a binary stream.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.