嘿,我有一个很长的字符串,我试图将它固定在一个固定大小的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'是不安全的,因为它不能保证目的地是空终止的。 –