我必须将一堆字符插入二进制文件。
为此,我使用 fwrite。在那之后,我想放一个\0
.
我不知道该怎么做。
const unsigned char *my_word; /*my_word has no \0 at the end*/
fwrite(my_word, my_word_length_in_bytes, 1, my_file);
你能帮忙吗?
谢谢。
您可以使用以下内容:
fputc(0, my_file);
您需要将缓冲区 my_word 分配为 +1,然后
my_word[my_word_length_in_bytes]=0;
fwrite(my_word, my_word_length_in_bytes+1, 1, my_file);
`fputc('\0', my_file);` will do it for you
一个老技巧是使用空的以 null 结尾的字符串
fwrite("", 1, 1, my_file);
您可以在word
实际将其写入文件之前在末尾连接一个 '\0' 字符(最好的解决方案恕我直言),或者使用 0x00 字节(这是 '\0' 的 ASCII 码)进行第二次 fwrite。