# 标准库stdio.h函数fgets() **函数原型** ```c char *fgets(char *str, int num, FILE * stream); ``` **说明** 从流中获取字符串。 这个函数可以取代C11废除的 `gets()` 函数。 **参数** ``` str: 一个字符数组指针 num: 最大字符数长度,包括结尾处的'\0'空字符 stream: 一个FILE类型的对象, 确定输入流 ``` **返回值** 返回一个C-style字符串。 **实例** ```c /* fgets example */ #include int main() { FILE * pFile; char mystring [100]; pFile = fopen ("myfile.txt" , "r"); if (pFile == NULL) perror ("Error opening file"); else { if ( fgets (mystring , 100 , pFile) != NULL ) puts (mystring); fclose (pFile); } return 0; } ```