c++arraysiostreamnull-character

How do I feed a byte array with null characters into a std::iostream?


I have to feed a byte array, which contains null characters, into a derivative of std::iostream. The raw data looks as follows:

FF 73 00 05 73

I stored this chunk of data into a char array:

char *tmc_command = new char [5];  // array size may vary
SerialStream tmc_receiver_handler; // a derivative of std::iostream

tmc_command [1] = 0xFF;
tmc_command [1] = 0x73;
tmc_command [2] = 0x00; // NULL character, eeeeeehhhh!!! :-(
tmc_command [3] = 0x05;
tmc_command [4] = 0x73;

instance -> tmc_receiver_handler << tmc_command;

When doing this, unfortunately, output stops at position 2 because of the null character.

As this is a mere byte array and not a real string, how can I make the stream object simply spit out the complete tmc_command without stopping at the null character? Or should I use a different object instead of the char []? Thank you.


Solution

  • The ostream inserter operators are for formatting. If you do not want to print your data as a formatted string, the formatted insertion operator is not what you want.

    Try using the ostream write() member function instead.