In order to use gets
safely, you have to know exactly how many characters you will be reading, so that you can make your buffer large enough. You will only know that if you know exactly what data you will be reading.
Instead of using gets
, you want to use fgets
, which has the signature
char* fgets(char *string, int length, FILE * stream);
(fgets
, if it reads an entire line, will leave the '
'
in the string; you'll have to deal with that.)
gets
remained an official part of the language up to the 1999 ISO C standard, but it was officially deprecated by the 2011 standard and removed in the 2014 standard. Most C implementations still support it, but at least gcc issues a warning for any code that uses it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…