操作 C 风格的字符串?

发布于 2024-10-04 05:29:39 字数 297 浏览 0 评论 0原文

嘿大家, 我的问题是,如何将两个 C 风格字符串附加到一个字符串中?

由于受 C++ 处理方式 (std::string) 的影响,我从未接触过 C 风格的字符串,并且需要为我当前的开发项目了解更多有关它们的信息。例如:

 char[] filename = "picture.png";
 char[] directory = "/rd/";
 //how would I "add" together directory and filename into one char[]?

提前致谢。

Hey all,
My question is, how do I append two C-style strings into one?

Being babied by the C++ way of doing things (std::string), I've never touched C-style strings and need to learn more about them for my current development project. For example:

 char[] filename = "picture.png";
 char[] directory = "/rd/";
 //how would I "add" together directory and filename into one char[]?

Thanks in advance.

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

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

发布评论

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

评论(6

墨离汐 2024-10-11 05:29:39
#include <stdlib.h>
#include <string.h>

// ...

char * fullpath;

fullpath = malloc(strlen(directory)+strlen(filename)+1);
if (fullpath == NULL)
{
  // memory allocation failure 
}
strcpy(fullpath, directory);
strcat(fullpath, filename);
#include <stdlib.h>
#include <string.h>

// ...

char * fullpath;

fullpath = malloc(strlen(directory)+strlen(filename)+1);
if (fullpath == NULL)
{
  // memory allocation failure 
}
strcpy(fullpath, directory);
strcat(fullpath, filename);
南街九尾狐 2024-10-11 05:29:39

您需要一个足够大的缓冲区,假设您在编译时没有方便的文件名和目录大小,则必须在运行时获取,像这样

char *buf = (char *) malloc (strlen (filename) + strlen (directory) + 1);
if (!buf) { /* no memory, typically */ }
strcpy (buf, filename);
strcat (buf, directory);

You need a big enough buffer, that, assuming you don't have filename's and directory's size handy at compile-time, you must get at run-time, like so

char *buf = (char *) malloc (strlen (filename) + strlen (directory) + 1);
if (!buf) { /* no memory, typically */ }
strcpy (buf, filename);
strcat (buf, directory);
尤怨 2024-10-11 05:29:39

请记住,您正在较低级别工作,它不会自动为您分配内存。您必须分配足够的内存来保存两个字符串和一个空终止符,然后将它们复制到位。

Keep in mind you're working a lower level, and it's not going to allocate memory for you automatically. You have to allocate enough memory to hold the two strings plus a null terminator and then copy them into place.

み青杉依旧 2024-10-11 05:29:39

请务必声明/分配一个足够大的 char 数组,以容纳 filenamedirectory。然后,按照 xxpor 的建议使用 strcat() (或 strncat())。

Be sure to declare/allocate a char array large enough to hold both filename and directory. Then, use strcat() (or strncat()) as xxpor suggested.

各自安好 2024-10-11 05:29:39

你必须考虑你的“字符串”在内存中实际上是如何表示的。在 C 中,字符串是分配内存的缓冲区,以 0 字节结尾。

filename  |p|i|c|t|u|r|e|0|
directory |/|r|d|/|0|

您需要的是一个新的内存空间来将两个字符串的内存内容以及最后一个 0 字节复制在一起。

path      |p|i|c|t|u|r|e|/|r|d|/|0|

给出了这段代码:(

int lenFilename = strlen(filename); // 7
int lenDirectory = strlen(directory); // 4
int lenPath = lenFilename + lenDirectory; // 11 I can count
char* path = malloc(lenPath + 1);
memcpy(path, filename, lenFilename);
memcpy(path + lenFilename, directory, lenDirectory);
path[lenPath] = 0; // Never EVER forget the terminating 0 !

...

free(path); // You should not forget to free allocated memory when you are done

这段代码中可能有一个相差 1 的错误,它没有经过实际测试......现在是凌晨 01:00,我应该去睡觉了!)

You have to think how your "string" is actually represented in memory. In C, strings are buffers of allocated memory terminated by a 0 byte.

filename  |p|i|c|t|u|r|e|0|
directory |/|r|d|/|0|

What you require is a new memory space to copy the memory content of both strings together and the last 0 byte.

path      |p|i|c|t|u|r|e|/|r|d|/|0|

Which gives this code:

int lenFilename = strlen(filename); // 7
int lenDirectory = strlen(directory); // 4
int lenPath = lenFilename + lenDirectory; // 11 I can count
char* path = malloc(lenPath + 1);
memcpy(path, filename, lenFilename);
memcpy(path + lenFilename, directory, lenDirectory);
path[lenPath] = 0; // Never EVER forget the terminating 0 !

...

free(path); // You should not forget to free allocated memory when you are done

(There may be an off-by-1 mistake in this code, it is not actually tested... It is 01:00am and I should go to sleep!)

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