C++ non null terminated char array outputting

后端 未结 5 611
刺人心
刺人心 2021-01-18 03:27

I was trying to output a not null terminated char array to a file.

Actual thing is, I am receiving packets and then printing their fields.

Now as these field

5条回答
  •  抹茶落季
    2021-01-18 04:00

    If you want to put exactly maxbytes bytes, use write method

    stream.write(buffer, maxbytes);
    

    If you can have less bytes in buffer, how do you know how many of them your buffer contains? If '\0' marks buffer end, you can write:

    stream.write(buffer, std::find(buffer, buffer+maxbytes, '\0') - buffer);
    

提交回复
热议问题