使用 uint8_t 数组进行 fwrite
我正在使用 fwrite 写入 uint8_t 类型的数组。在终端中,运行程序后,当我使用
cat file.txt
所有内容时,都会按应有的方式打印。 但是当我打开文件时,
vim file.txt
我得到了一个带有一堆乱码的超级文件,主要是vim的高重复性
^@^@^@^@^@^@...
打开窗口上下文的底部注释了[noeol]
还在问题的 :udp客户端/服务器文件复制程序。我需要原始文件与新文件进行比较;但事实并非如此。
uint8_t buf[...];
recvfrom(...buf...);
fwrite(buf...);
原始文件有约 150 个字符,乱码文件有约 30k 个字符。
我将不胜感激任何类型的答案或方向
-austin
i'm using fwrite to write an array of type uint8_t. in the terminal, after running the program, when i use
cat file.txt
everything prints as it should be.
but when i open the file with
vim file.txt
i get a super whack file with a bunch of jibberish, mainly a high repitition of
^@^@^@^@^@^@...
vim also notes [noeol] at the bottom of the open window
context of problem: udp client/server file copy program. i need the original file to diff with my new file; which it does not.
uint8_t buf[...];
recvfrom(...buf...);
fwrite(buf...);
the original file has ~150 characters, the jibberish file has ~ 30k characters.
i would appreciate any kind of answer or direction
-austin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当vim在文件中遇到不可打印的字符时,它使用转义码来表示它。
^@
转义码用于表示空字符(即C中的'\0'
)。该字符不可打印,当您执行cat file.txt
时,控制台会丢弃它。所以我认为您的代码向
fwrite
调用传递了不正确的大小。是否传递缓冲区的大小而不是接收到的数据的大小?您的代码应如下所示(经过适当的错误检查):
When vim encounter a non-printable character in a file, it use an escape code to represent it. The
^@
escape code is used to represent the null character (i.e.'\0'
in C). This character being non-printable, the console just discard it when you docat file.txt
.So I think that your code is passing an incorrect size to the
fwrite
call. Are passing the size of the buffer instead of the size of the received data?Your code should read something like (with proper error checking):