C++ non null terminated char array outputting

后端 未结 5 544
刺人心
刺人心 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:46

    I see mainly two solutions.

    In case of ASCII data:

    memset(dest,0,destlength); 
    bytescopied = strncpy(dest, src, maxbytes);
    

    then You'll always have clear null-terminated string in buffor.

    Second in case of ASCII data:

    std::string yourASCII(src,maxbytes);
    yourASCII.c_str() // would be null terminated.
    

提交回复
热议问题