读取系统呼叫和无效的字符
如果我使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
绝对不是。
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()
andgetc()
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 tostrlen()
. You need to keep track of that yourself. The other basicstr*
functions typically havemem*
functions that do the same thing where you pass the length of an argument. In particular, we havememcpy(), memmem(), memchr()
. There's nomemcat()
but synthesis is trivial.