11.2 字符串和字符串函数:fgets()函数

来源:互联网 发布:高要网络问政平台 编辑:程序博客网 时间:2024/06/06 18:31

fgets()函数的第二个参数指明了读入字符的最大数量,如果读到一个换行符,会把它存在字符串中,同时,fgets()函数并不会自动换行,其第三个参数指明要读入的文件,如果从键盘输入,则是stdin(标准输入)作为参数。

#include<stdio.h>#define STLEN 10int main(){    char words[STLEN];    puts("enter string :");    while (fgets(words, STLEN, stdin) != NULL&&words[0] != '\n')    {        fputs(words, stdout);    }    puts("done.");    return 0;}

输出示例:

enter string :by the way,the gets() function by the way,the gets() function done.