读取系统呼叫和无效的字符

发布于 2025-01-20 03:07:38 字数 193 浏览 2 评论 0原文

如果我使用read()系统调用buffer read read read()将使用read()系统调用从文件中读取文件buffer中的null终止字符?还是我需要在文件中隐式包含零终止的字符,以便将其添加到buffer中?

If I read from a file with a certain file descriptor using the read() system call into a buffer, does read() will add a null-terminated character in the buffer? Or do I need to implicitly include the null-terminated character in the file so that it will be added into the buffer?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清晰传感 2025-01-27 03:07:38

绝对不是。 read()使用带有空字符的二进制文件。

一般而言,当处理文本时,我们只是假设其中没有任何空字符。但是有些程序实际上对其进行了处理。 read> read()getc()可以很好地处理nulls。正是弦库对它们不满意或缺乏它们。

因此,不,不要将您的read()缓冲区传递到strlen()。您需要自己跟踪。其他基本str*函数通常具有mem*函数,可以在传递参数长度的情况下执行相同的操作。特别是,我们具有memcpy(),memmem(),memchr()。没有memcat(),但合成是微不足道的。

Definitely not. read() handles binary files with can contain null characters.

Generally speaking, when processing text we just assume there aren't any null characters in it; but some programs actually process them. Both read() and getc() handle nulls just fine. It's the string libraries that aren't happy with them or lack of them.

So, no, don't pass your read() buffer to strlen(). You need to keep track of that yourself. The other basic str* functions typically have mem* functions that do the same thing where you pass the length of an argument. In particular, we have memcpy(), memmem(), memchr(). There's no memcat() but synthesis is trivial.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文