

fgets()                   STDIO Function                  fgets()




Read line from stream

#include <stdio.h>
cchhaarr *ffggeettss(_s, _n, _f_p) cchhaarr *_s; iinntt _n; FFIILLEE *_f_p;

fgets reads  characters from  the stream  fp into string  s until
either _n-1 characters have been read,  or a newline or EOF is en-
countered.  It  retains the newline,  if any, and  appends a null
character at  the end of of the string.   fgets returns the argu-
ment _s if any characters were read, and NULL if none were read.

***** Example *****

This example  looks for the pattern given  by argv[1] in standard
input or in file argv[2].  It demonstrates the functions pnmatch,
fgets, and freopen.


#include <stdio.h>
#define MAXLINE 128
char buf[MAXLINE];



void fatal(s) char *s;
{
        fprintf(stderr, "pnmatch: %s\n", s);
        exit(1);
}



main(argc, argv)
int argc; char *argv[];
{
        if (argc != 2 && argc != 3)
                fatal("Usage: pnmatch pattern [ file ]");



        if (argc==3 && freopen(argv[2], "r", stdin)==NULL)
                fatal("cannot open input file");



        while (fgets(buf, MAXLINE, stdin) != NULL) {
                if (pnmatch(buf, argv[1], 1))
                        printf("%s", buf);
        }







COHERENT Lexicon                                           Page 1




fgets()                   STDIO Function                  fgets()



        if (!feof(stdin))
                fatal("read error");
        exit(0);
}


***** See Also *****

fgetc(), gets(), STDIO

***** Diagnostics *****

fgets returns NULL  if an error occurs, or if  EOF is seen before
any characters are read.











































COHERENT Lexicon                                           Page 2


