2013-03-10 145 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