更新时间:2023-11-13 19:16:34
要获取用户输入的行,请使用 fgets()
.
To get a line of user input, use fgets()
.
#define S_MAX_LENGTH
char s[S_MAX_LENGTH + 2];
if (fgets(s, sizeof s, stdin)) {
s[strcspn(s, "\n")] = '\0'; // Should code want to lop off a potential trailing \n
....
请勿使用 scanf(%[^ \ n] s",s);
或 gets(s);
.它们遭受缓冲区溢出和其他问题的困扰.
Do not use scanf("%[^\n]s", s);
nor gets(s);
. They suffer from buffer overflow and other issues.