嘿,我有一個很長的字符串,我試圖將它固定在一個固定大小的char數組中。我不在乎,如果字符串被砍掉,我只是想要char數組的每個元素有東西。C++如何將大字符串放入固定char數組中
例如
char first_ten_alaphabet[10];
string str = "abcdefghijklnopqrstuvwxyz";
strcpy(first_ten_alaphabet, str.c_str()); //<-- this will cause program to break
任何幫助將是不錯的感謝
也許'strncpy()函數(first_ten_alaphabet,10);'?或者,最好添加'first_ten_alphabet [9] ='\ 0';'。 – FoggyDay
@FoggyDay'strncpy'是不安全的,因爲它不能保證目的地是空終止的。 –