在c中连接多个字符串的更好方法?
除了连续多次调用 strcat() 之外,是否有更好的方法在 c 中将多个字符串连接在一起,如下所示?
char prefix[100] = "";
strcat(prefix, argv[0]);
strcat(prefix, ": ");
strcat(prefix, cmd_argv[0]);
strcat(prefix, ": ");
strcat(prefix, cmd_argv[1]);
perror(prefix);
Is there a better way to concatenate multiple strings together in c other than having multiple calls to strcat() all in a row, like below?
char prefix[100] = "";
strcat(prefix, argv[0]);
strcat(prefix, ": ");
strcat(prefix, cmd_argv[0]);
strcat(prefix, ": ");
strcat(prefix, cmd_argv[1]);
perror(prefix);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
或者使用
snprintf
来防止缓冲区溢出。Or
snprintf
to prevent buffer overruns.snprintf 将是最好且最容易使用的选项,尽管它可能不是“快”。你没有说明你的标准是什么。不过,简单性绝对是这样的:
snprintf would be the best and easiest to use option, though it may not be "fast". You didn't state what your criteria was. Simplicity is definitely this, though:
我会像其他人建议的那样使用 sprintf() ,但这是为了完整性:
如果您有
的便利之处在于它可以被“链接”,如上所述。此外,由于
stpcpy()
返回指向结果字符串末尾的指针,因此后续stpcpy()
调用不需要一次又一次地遍历旧数据。因此,它比多个strcat()
更高效,并且可能比sprintf()
更高效。stpcpy()
是 POSIX:2008。I would use
sprintf()
like others have suggested, but this is for completeness:If you have
stpcpy()
, then you can do:The convenience with
stpcpy()
is that it can be "chained", as above. Also, sincestpcpy()
returns a pointer to the end of the resultant string, subsequentstpcpy()
calls don't need to go through the old data again and again. So, it is more efficient than multiplestrcat()
s and probably more efficient thansprintf()
.stpcpy()
is POSIX:2008.我可能会因此而受到批评,但那又怎样呢。可能发生的最糟糕的事情是我会学到一些东西。
我现在并没有真正使用 C,而且我通常不在 C++ 中使用 C 风格的字符串。但我的一个想法是编写一个修改后的 strcpy() 来返回字符串的结尾:
现在 Shlemiel 可以随身携带他的水桶:
我还没有测试过这一点。评论?
编辑:删除了不必要的
my_strcat()
函数。而且它与stpcpy()
相同,后者显然是 2008 年 POSIX 的一部分。请参阅 http://www.manpagez.com/man/3/stpcpy/。I might take a rep hit for this, but what the heck. The worst thing that can happen is I'll learn something.
I don't really use C these days, and I don't typically use C-style strings in C++. But one idea I have is to write a modified strcpy() that returns the end of the string:
Now Shlemiel can bring his bucket along with him:
I haven't tested this. Comments?
EDIT: Removed the unnecessary
my_strcat()
function. Also it turns out to be the same asstpcpy()
, which is apparently part of POSIX as of 2008. See http://www.manpagez.com/man/3/stpcpy/.如果您尝试从其他字符串构建字符串(您的示例建议),那么您可以使用 snprintf。
如果您尝试对现有字符串进行串联,但无法使用格式方法,那么您可能会陷入对 strcat 的多次调用,尽管我强烈建议您可能要考虑使用
strncat
相反,并检查以确保没有缓冲区溢出。If you're trying to build a string from other strings (which your example suggests), then you can use snprintf.
If you're trying to do concatenation of an existing string, where you can't use the format approach, then you're probably stuck with multiple calls to strcat, although I'd strongly suggest that you might want to consider using
strncat
instead and checking to ensure you don't have buffer overruns.假设您发布的是 char[fixed_size],而不是 char*,您可以使用单个创意宏以类似 cout 的顺序而不是脱节的 printf 样式格式一次性完成所有操作。如果您正在使用嵌入式系统,此方法将允许您省略大型 printf 系列函数,例如
snprintf()
(这也可以防止 Dietlibc 抱怨),甚至不需要malloc()
或
中的任何函数。Assuming you have a char[fixed_size] as posted, rather than a char*, you can use a single, creative macro to do it all at once with a cout-like ordering rather than the disjointed printf style format. If you are working with embedded systems, this method will allow you to leave out the large printf family of functions like
snprintf()
(This keeps dietlibc from complaining too) and doesn't even requiremalloc()
or any functions from<string.h>
.您可以使用 snprintf 函数
you can use snprintf function
就不能用宏吗?
Couldn't you use a Macro.