读入字符串

用scanf时,一般情况下只能读取一个字符串,且遇空格终止。

此时若想读入像“I am a student."含空格的字符串时就要使用fgets了。

原型是char *fgets(char *s, int n, FILE *stream);  

参数:  

*s: 字符型指针,指向将存储到的数据地址。  

n: 整型数据,将从流中读取 n - 1 个字符。  

*stream: 指针数据,欲读取的流。  

功能:  从文件指针stream中读取n-1个字符,存到以s为起始地址的空间里,直到读完一行,如果成功则返回s的指针,否则返回NULL。

例如,从终端读入:

char str[100];

fgets(str,100,stdin);

fputs(str,stdout);

输入:I am a student.

输出:I am a student.

原文地址:https://www.cnblogs.com/tanhehe/p/2883537.html