2013-03-10 154 views
48

我有一个私人类变量(char name [10]),我想添加.txt扩展名,以便打开目录中的文件。我如何去做这件事?最好创建一个包含连接字符串的新字符串变量。如何在C++中连接两个字符串?

回答

86

首先,不要使用char*char[N]。使用std::string,那么其他一切都变得如此简单!

例子,

std::string s = "Hello"; 
std::string greet = s + " World"; //concatenation easy! 

简单,不是吗?

现在,如果你需要char const *出于某种原因,当你想传递给一些功能,例如,那么你可以这样做:

some_c_api(s.c_str(), s.size()); 

假设这个函数声明为:

some_c_api(char const *input, size_t length); 

浏览std::string你自己从这里开始:

希望有帮助。

+0

这是什么字符串连接的时间复杂度? – d34th4ck3r 2016-12-17 13:46:21

+0

@ d34th4ck3r:它是线性的。 – Nawaz 2016-12-17 14:19:01

11

既然是C++,为什么不使用std::string而不是char*? 级联将是微不足道的:

所有的
std::string str = "abc"; 
str += "another"; 
5

移植的C库中有一个strcat()函数,它将为您执行“C风格字符串”级联。

顺便说一句,即使C++有一堆的函数来处理C风格的字符串,它可能是有益的你尝试,并拿出自己的函数,这样做,是这样的:

char * con(const char * first, const char * second) { 
    int l1 = 0, l2 = 0; 
    const char * f = first, * l = second; 

    // step 1 - find lengths (you can also use strlen) 
    while (*f++) ++l1; 
    while (*l++) ++l2; 

    char *result = new char[l1 + l2]; 

    // then concatenate 
    for (int i = 0; i < l1; i++) result[i] = first[i]; 
    for (int i = l1; i < l1 + l2; i++) result[i] = second[i - l1]; 

    // finally, "cap" result with terminating null char 
    result[l1+l2] = '\0'; 
    return result; 
} 

...然后...

char s1[] = "file_name"; 
char *c = con(s1, ".txt"); 

...其结果是file_name.txt

你也可能会试图编写自己的operator +但是只有指针的IIRC操作符重载,因为参数是不允许的。

另外,不要忘记这种情况下的结果是动态分配的,所以你可能想调用delete来避免内存泄漏,或者你可以修改函数来使用栈分配的字符数组,当然提供它有足够的长度。

+0

查看我对IcyFlame的回答的评论。 – TonyK 2013-03-10 07:25:22

+3

还有'strncat()'函数通常是更好的选择 – nogard 2013-03-10 07:25:29

+0

@nogard:'strncat'在这里是不相关的,因为我们已经知道第二个参数'“.txt”'的长度。所以它只会是'strncat(name,“.txt”,4)',这对我们没有任何好处。 – TonyK 2013-03-10 07:44:31

6

如果你在C语言进行编程,然后假设name真的是一个固定长度的数组像你说的,你必须做类似如下:

char filename[sizeof(name) + 4]; 
strcpy (filename, name) ; 
strcat (filename, ".txt") ; 
FILE* fp = fopen (filename,... 

现在你明白为什么大家都建​​议std::string

+1

你不能看到那个人在C++中提出了一个问题,而不是在C? – IcyFlame 2013-03-10 07:32:04

+9

你不能看到我说“如果你正在编程...”吗? – TonyK 2013-03-10 07:32:57

+0

难道你不觉得这个答案与这个问题无关吗? – IcyFlame 2013-03-10 07:33:26

-2
//String appending 
#include<iostream> 
using namespace std; 
void stringconcat(char *str1, char *str2){ 
while (*str1 != '\0'){ 
    str1++; 
} 
while(*str2 != '\0'){ 
    *str1 = *str2; 
    str1++; 
    str2++; 
} 
return ;  
} 
int main( ){ 
char str1[100]; 
cin.getline(str1,100); 
char str2[100]; 
cin.getline(str2,100); 
stringconcat(str1,str2); 
cout<<str1; 
getchar(); 
return 0; 
} 
+1

..........我甚至看到了什么? – 2017-09-06 13:47:11

0

这是更好地使用C++字符串类,而不是老式的C字符串查找,生活会更容易。

如果您有现有的旧样式的字符串,你可以隐蔽的串级

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; 
    cout<<greeting + "and there \n"; //will not compile because concat does \n not work on old C style string 
    string trueString = string (greeting); 
    cout << trueString + "and there \n"; // compiles fine 
    cout << trueString + 'c'; // this will be fine too. if one of the operand if C++ string, this will work too