-1

我必须将一堆字符插入二进制文件。

为此,我使用 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);

你能帮忙吗?

谢谢。

4

5 回答 5

2

您可以使用以下内容:

fputc(0, my_file);
于 2011-03-25T11:26:17.640 回答
1

您需要将缓冲区 my_word 分配为 +1,然后

my_word[my_word_length_in_bytes]=0;
fwrite(my_word, my_word_length_in_bytes+1, 1, my_file);
于 2011-03-25T11:28:16.953 回答
0
`fputc('\0', my_file);` will do it for you
于 2011-03-25T11:26:36.797 回答
0

一个老技巧是使用空的以 null 结尾的字符串

fwrite("", 1, 1, my_file);
于 2011-03-25T11:26:37.517 回答
0

您可以在word实际将其写入文件之前在末尾连接一个 '\0' 字符(最好的解决方案恕我直言),或者使用 0x00 字节(这是 '\0' 的 ASCII 码)进行第二次 fwrite。

于 2011-03-25T11:28:38.320 回答