If I want to read in a string of arbitrary length from the command line, what's the best way of going about it?
At the moment I'm doing this:
char name_buffer [ 80 ];
int chars_read = 0;
while ( ( chars_read < 80 ) && ( !feof( stdin ) ) ) {
name_buffer [ chars_read ] = fgetc ( stdin );
chars_read++;
}
But what can I do if the string is longer than 80 characters? Obviously I could just initialise the array to a bigger number but I'm sure there must be a better way to give the array more space using malloc or something?
Any hints would be great.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…