C++ non null terminated char array outputting

后端 未结 5 589
刺人心
刺人心 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 03:57

    If you don't care about the last byte, you can just

    buffer[buffersize-1] = 0;
    

    and then feed buffer to whatever string function you want. If it is shorter, everything will run to the null terminator that already exists, and if there was no terminator it will run to the one you just created.

    And it's fast :)

提交回复
热议问题