.TH fgets() "" "" "STDIO Function (libc)"
.PC "Read line from stream"
.B "#include <stdio.h>"
\fBchar *fgets(\fIs\^\fB, \fIn\^\fB, \fIfp\^\fB)
char *\fIs\^\fB; int \fIn\^\fB; FILE *\fIfp\^\fB;\fR
.PP
.B fgets()
reads characters from the stream
.I fp
into string
.I s
until either \fIn\fR\-1 characters have been read, or
a newline or EOF is encountered.
It retains the newline, if any, and appends a null character
at the end of of the string.
.B fgets()
returns the argument \fIs\fR
if any characters were read, and NULL if none were read.
.SH Example
This example looks
for the pattern given by
.B argv[1]
in standard input or in file
.BR argv[2] .
It demonstrates the functions
.BR pnmatch() ,
.BR fgets() ,
and
.BR freopen() .
.DM
#include <stdio.h>
#define MAXLINE 128
char buf[MAXLINE];
.DE
.DM
void fatal(s) char *s;
{
	fprintf(stderr, "pnmatch: %s\en", s);
	exit(1);
}
.DE
.DM
main(argc, argv)
int argc; char *argv[];
{
	if (argc != 2 && argc != 3)
		fatal("Usage: pnmatch pattern [ file ]");
.DE
.DM
	if (argc==3 && freopen(argv[2], "r", stdin)==NULL)
		fatal("cannot open input file");
.DE
.DM
	while (fgets(buf, MAXLINE, stdin) != NULL) {
		if (pnmatch(buf, argv[1], 1))
			printf("%s", buf);
	}
.DE
.DM
	if (!feof(stdin))
		fatal("read error");
	exit(0);
}
.DE
.SH "See Also"
.Xr "fgetc()," fgetc
.Xr "gets()," gets
.Xr "libc" libc
.br
\*(AS, \(sc7.9.7.2
.br
\*(PX Standard, \(sc8.1
.SH Diagnostics
.B fgets()
returns NULL if an error occurs, or if
.B EOF
is seen before any characters are read.
