C 中的 sprintf() 没有尾随空空格
有没有办法使用 C sprintf() 函数而不在其输出末尾添加 '\0' 字符? 我需要在固定宽度字符串的中间写入格式化文本。
Is there a way to use the C sprintf() function without it adding a '\0' character at the end of its output? I need to write formatted text in the middle of a fixed width string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
无法告诉 sprintf() 不写入尾随 null。 您可以做的是使用 sprintf() 写入临时字符串,然后使用 strncpy() 之类的方法仅复制所需的字节。
There is no way to tell
sprintf()
not to write a trailing null. What you can do is usesprintf()
to write to a temporary string, and then something likestrncpy()
to copy only the bytes that you want.sprintf 返回写入的字符串的长度(不包括空终端),您可以使用它来知道空终端在哪里,并将空终端字符更改为其他字符(即空格)。 这比使用 strncpy 更有效。
sprintf returns the length of the string written (not including the null terminal), you could use that to know where the null terminal was, and change the null terminal character to something else (ie a space). That would be more efficient than using strncpy.
您无法使用 sprintf() 执行此操作,但可以使用 snprintf() 执行此操作,具体取决于您的平台。
您需要知道要替换多少个字符(但是当您将它们放入字符串中间时,您可能知道这一点)。
这是有效的,因为 snprintf() 的某些实现不保证写入终止字符 - 大概是为了与 stncpy() 等函数兼容。
此后,“123”被替换为“Joe”。
在 snprintf() 保证空终止的实现中,即使字符串被截断,这也不起作用。 因此,如果代码可移植性是一个问题,您应该避免这种情况。
大多数基于 Windows 的版本 snprintf() 都表现出这种行为。
但是,MacOS 和 BSD(也许还有 linux) ) 似乎总是以空终止。
You can't do this with sprintf(), but you may be able to with snprintf(), depending on your platform.
You need to know how many characters you are replacing (but as you're putting them into the middle of a string, you probably know that anyway).
This works because some implementations of snprintf() do NOT guarantee that a terminating character is written - presumably for compatibility with functions like stncpy().
After this, "123" is replaced with "Joe".
On implementations where snprintf() guarantees null termination even if the string is truncated, this won't work. So if code portability is a concern, you should avoid this.
Most Windows-based versions of snprintf() exhibit this behaviour.
But, MacOS and BSD (and maybe linux) appear to always null-terminate.
您还可以使用固定宽度字符串作为格式字符串,如下所示:
应该产生
You could also use your fixed width string as a format string like this:
should yield
由于您要写入固定区域,因此可以这样做:
Since you're writing to a fixed area, you can do it like this:
这是内存受限设备的一个选项。 它以牺牲速度来换取使用更少的 RAM。 有时我必须这样做来更新打印到 LCD 的字符串的中间部分。
这个想法是,您首先使用零大小的缓冲区调用 snprintf 来确定哪个索引将被空终止符破坏。
您可以在此处运行以下代码: https://rextester.com/AMOOC49082
打印
buf:123de
Here's an option for memory constrained devices. It trades off speed for using less RAM. I sometimes have to do this to update the middle of a string that gets printed to a LCD.
The idea is that you first call snprintf with a zero sized buffer to determine which index will get clobbered by the null terminator.
You can run the below code here: https://rextester.com/AMOOC49082
Prints
buf:123de
实际上,如果您使用 snprintf,此示例将不会添加 null:
Actually this example will not add a null if you use snprintf:
看这里:http://en.wikipedia.org/wiki/Printf
look here: http://en.wikipedia.org/wiki/Printf