将字符串复制到字符数组中,务必检查数组是否足够大,检查数组的容量。
#include <iostream>
#include <string>
#include <string.h>
using namespace std;
int main(){
char buffer[20]={'\0'};
cout << "Enter a line of text: " <<endl;
string lineentered;
getline(cin,lineentered);
if(lineentered.length()<20){
strcpy(buffer,lineentered.c_str());
cout << "buffer contains: " << buffer <<endl;
}
return 0;
}