我想知道是否可能读取一个未知大小的字符串,直到空格或换行符被遇到。在C++中,我做了以下操作:
char * dynStr;
char buffer[20];
cin >> buffer;
dynStr = new char[strlen(buffer) +1];
strcpy(dynStr, buffer);
但问题是,如果输入的内容大于20怎么办?因此,我认为应该像这样做:
do
{
cin.get ( buffer, 20, ' '); //im not sure this is the right approach
strcpy(.....); // if is not a first iteration
//add the new buffer to the end of dyn str..
} while( ! read ' ' or '\n' ) <--- this is what I have problem doing
我知道在c++中可以使用std::string
,但我想知道如何做到这一点,如果您有任何想法,请告诉我 :)
std::string
,而且应该使用它。 - crashmstrcin >> std::string
,但不使用std::string
。 - HolyBlackCat