sgetn不null终止字符串 [英] sgetn Doesn't Null Terminate String

查看:220
本文介绍了sgetn不null终止字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sgetn 获取 char * 作为它的第一个参数,并向其中写入字符。它不会在 char * 中写入结尾'\0' / p>

这种行为似乎与其他时间不一致,我可以找到 char * 写入。但是, 在Clang,gcc和Visual Studio中是一致的,所以我不敢相信这是所有编译器都有的错误。



是否有一个原因,标准不要求'\0' char *



[ Live Example ]

解决方案

因为它可以用来读取任意数据,而不只是文本。



以eg std :: istream :: read 函数,它还采用 char * 参数,但可以用于读取任意数据,包括二进制数据。你不希望它从二进制文件中读取时添加一个字符串终止符?


sgetn Takes a char* for it's first argument and writes characters to it. It does not write a trailing '\0' to the char*.

This behavior seems to be inconsistent with every other time that I can find a char* written to. However, it is consistent across Clang, gcc, and Visual Studio, so I can't believe it's a bug that all the compilers have.

Is there a reason that the standard doesn't require the trailing '\0' to the char*?

[Live Example]

解决方案

Because it can be used to read arbitrary data, not just text.

Take e.g. the std::istream::read function, it also takes a char* argument, but can be used to read arbitrary data, including binary data. You would not expect it to add a string terminator when reading from a binary file?

这篇关于sgetn不null终止字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆