如何在C语言的二进制文件中插入\0
我必须将一堆字符插入到二进制文件中。
为了做到这一点,我使用 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);
你能帮忙吗?
谢谢。
I have to insert a bunch of chars to a binary file.
In order to do that I use fwrite. After that, I would like to put a \0
.
I am not sure how to do it.
const unsigned char *my_word; /*my_word has no \0 at the end*/
fwrite(my_word, my_word_length_in_bytes, 1, my_file);
Can you please help?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用以下内容:
You can use the following:
您需要将缓冲区 my_word 分配为 +1,然后
You need to have your buffer my_word alocated with +1, then
一个老技巧是使用空的以 null 结尾的字符串
An old trick is to use an empty null-terminated string
您可以在将
word
实际写入文件之前在其末尾连接一个 '\0' 字符(恕我直言,最好的解决方案),或者使用 0x00 字节(这是 ASCII 代码)进行第二次 fwrite '\0')。You can either concatenate a '\0' character at the end of
word
before you actually write it into your file (best solution imho) or do a second fwrite with a 0x00 byte (which is ASCII code for '\0').