fgets(3C)
_________________________________________________________________
fgets function
Read a string from a specified file.
_________________________________________________________________
Calling Sequence
#include <stdio.h>
FILE *fp;
char *stringbuf, *fgets();
int max;
fgets(stringbuf, max, fp);
where stringbuf is a pointer to a user-provided buffer.
max specifies the maximum number of characters to
read.
fp is a pointer to a stream.
Description
The fgets function reads a string from a file you specify. The
read halts at the number of characters you specify in max, or a
New Line , whichever comes first, reading max-1 characters into
the buffer and appending a null.
The include file stdio.h defines fgets.
Returns
The fgets function returns a null at end of file or an error, or
stringbuf if no error occurs.
Related Functions
See also the dg_fgets, fgetc, fputs, getc, getchar, gets, getw,
and ungetc functions.
Example
/* Program test for the fgets() function */
DG/UX 4.00 Page 1
Licensed material--property of copyright holder(s)
fgets(3C)
#include <stdio.h>
FILE *fopen(), *fp;
char string[6], *fgets(), err;
int i = 1, max;
main(argc, argv)
int argc;
char *argv[];
{
max = 6;
while (i < argc) {
fp = fopen(argv[i], "r");
err = fgets(string, max, fp);
printf("%s\n", string);
i++;
}
}
A call to the program test with the valid filenames a, b, and c,
which contain respectively
aaaaaaa
bbbbb
ccc
generates the output
aaaaa
bbbbb
ccc
DG/UX 4.00 Page 2
Licensed material--property of copyright holder(s)