fgets C · 输入输出
从指定文件流最多读取一行字符串,能限制最大长度,防止溢出。
怎么用
fgets(字符数组, 最大长度, 文件指针);
小例子
#include <stdio.h>
#include <string.h>
int main(void) {
char line[50];
fgets(line, sizeof(line), stdin);
printf("%lu", (unsigned long)strlen(line));
return 0;
}
运行结果
输入 hello 后输出 6(包含末尾换行符)
提醒:会保留输入里的换行符;读取失败返回 NULL
在代码里遇到不认识的词?打开代码点读机,点一下就懂 → ← C全部词条