简体   繁体   English

用C / C ++连接字符串

[英]Concatenate Strings in C/C++

How do I concatenate Strings with C/C++? 如何将字符串与C / C ++连接?

I tried the following ways: 我尝试了以下方法:

PS: errorInfo is a char * I should return it. PS:errorInfo是一个char *我应该返回它。

        errorInfo = strcat("Workflow: ", strcat(
            workflowToString(workflow).utf8(), strcat(" ERROR: ",
                    errorCode.utf8)));

        sprintf(errorInfo, "Workflow %s ERROR: %s",
            workflowToString(workflow).utf8(), errorCode.utf8());

        errorInfo = "Workflow: " + workflowToString(workflow).utf8() + " ERROR: " + errorCode.utf8;

Just the sprintf compiles but when running my application crash. 只是sprintf编译,但运行我的应用程序崩溃。

PS: I'm using NDK from Android PS:我正在使用Android的NDK

There ISN'T such a language as C/C++. 没有这样的语言如C / C ++。 There is C, and there is C++. 有C,有C ++。

  • In C++ you concatenate std::string 's by using operator+ 在C ++中,您使用operator+连接std::string
  • In C, you use strcat 在C中,您使用strcat

I know this doesn't quite answer your question, this is just an outcry :) 我知道这并不能完全回答你的问题,这只是一个哗然:)

According to this page strcat does the following: 根据这个页面, strcat执行以下操作:

Appends a copy of the source string to the destination string. 将源字符串的副本附加到目标字符串。 The terminating null character in destination is overwritten by the first character of source, and a new null-character is appended at the end of the new string formed by the concatenation of both in destination. 目标中的终止空字符被源的第一个字符覆盖,并且新的空字符附加在由目标中的两个串联形成的新字符串的末尾。

In your implementation, however, "Workflow: " is a constant string. 但是,在您的实现中, "Workflow: "是一个常量字符串。 You cannot modify that string, which is what strcat would do. 你不能修改那个字符串,这是strcat会做的。 In order to do that, create a string like: 为此,请创建一个字符串,如:

char message[1000];
strcpy(message, "Workflow: ");
strcat(message, "other string");
....

However, be careful about the utf8 character encoding because one utf8 code point could be multiple char s long. 但是,要注意utf8字符编码,因为一个utf8代码点可能是多个char长。

Concatenation is almost always the wrong idiom for string building, especially in C. It's error-prone, clutters your code, and has extremely bad asymptotic performance (ie O(n^2) instead of O(n) for building a string of length n ). 连接几乎总是错误的字符串构建习惯,特别是在C语言中。它容易出错,使代码混乱,并且具有极差的渐近性能(即O(n^2)而不是O(n)用于构建长度字符串n )。

Instead you should use the snprintf function, as in: 相反,您应该使用snprintf函数,如:

snprintf(buf, sizeof buf, "Workflow: %s ERROR: %s", workflow, error);

or if you're writing to a file/socket/etc. 或者如果您正在写入文件/套接字/等。 and don't need to keep the resulting string in memory, simply use fprintf to begin with. 并且不需要将结果字符串保存在内存中,只需使用fprintf即可。

There are many ways you can concatenate in C while using Android NDK: 在使用Android NDK时,有很多方法可以在C中连接:

Two ways I used are: 我使用的两种方法是:

  • strcat strcat的
  • sprintf 的sprintf

here is example: 这是一个例子:

enter code here 在这里输入代码

strcat strcat的

char* buffer1=(char*)malloc(250000);
char* buffer2=(char*)malloc(250000);
char* buffer3=(char*)malloc(250000);

buffer1 = strcat(buffer1, buffer2);

sprintf 的sprintf

sprintf(buffer3,"this is buffer1: %s and this is buffer2:%s",buffer1,buffer2);`

sprintf returns length of your string sprintf返回字符串的长度

strcat is not recommended as its use more memory.. you can use sprintf or others like strcpy. 不建议使用strcat,因为它使用更多的内存..你可以使用sprintf或其他类似strcpy。

Hope it helps. 希望能帮助到你。

By using strcat() , you are working in c, not c++. 通过使用strcat() ,您使用的是c,而不是c ++。 c is not going to automatically manage memory for you. c不会自动为您管理内存。 c can be confusing since sometimes it seems like it has a string data type when all it is doing is providing you a string interface to arrays of characters. c可能会让人感到困惑,因为有时看起来它有一个字符串数据类型,它所做的就是为字符数组提供一个字符串接口。 For one thing, the first argument to strcat() has to be writable and have enough room to add the second string. 首先, strcat()的第一个参数必须是可写的,并且有足够的空间来添加第二个字符串。

char *out = strcat("This", "nThat");

is asking c to stomp on string literal memory. 要求c踩踏字符串文字内存。

In general, you should NEVER use strcat()/sprintf , as in the above "chosen" answer. 一般情况下,你不应该使用strcat()/sprintf ,就像上面的“选择”答案一样。 You can overwrite memory that way. 你可以用这种方式覆盖内存。 Use strncat()/snprintf() instead to avoid buffer overruns. 使用strncat()/snprintf()来避免缓冲区溢出。 If you don't know the size to pass to "n" in strncat() , you're likely doing something wrong. 如果你不知道在strncat()传递给“n”的大小,你可能做错了什么。

One way to do this in c would be: 在c中执行此操作的一种方法是:

 #define ERROR_BUF_SIZE  2048  // or something big enough, you have to know in c




char errorInfo[ERROR_BUF_SIZE];

   snprintf(errorInfo, ERROR_BUF_SIZE, "Workflow %s ERROR: %s",
            workflowToString(workflow).utf8(), errorCode.utf8());

or similarly using strncpy/strncat 或类似地使用strncpy/strncat

With string literals you can simple use: 使用字符串文字,您可以简单地使用:

char str[] = "foo" " bar";
const char *s = " 1 " " 2 ";
s = " 3 " " 4 ";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM