Reads a line from the specified file, up to one less than the specified maximum number of characters or up to and including the new-line character, whichever comes first. The function stores the string in str.
#include <stdio.h> char *fgets (char *str, int maxchar, FILE *file_ptr);Function Variants This function also has variants named _fgets32 and _fgets64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.8 for more information on using pointer-size-specific functions.
When the file pointed to by file_ptr is opened in record mode, fgets treats the end of a record the same as a new-line character, so it reads up to and including a new-line character or to the end of the record.
x | Pointer to str. |
NULL | Indicates the end-of-file or an error. The contents of str are undefined if a read error occurs. |
#include <stdio.h> #include <stdlib.h> main() { FILE *fp; char c_ptr[130]; /* Open a file with some data -"THIS IS A TEST." */ if ((fp = fopen ("file.dat","r+") ) == NULL) perror ("open error"),exit(1); fgets(c_ptr,130,fp); puts(c_ptr); /* Display what fgets got. */ fclose(fp); }