HP C/iX Library Reference Manual (30026-90004)
Chapter 5 151
HP C/iX Library Function Descriptions
fgets
fgets
Reads a string from an open stream.
Syntax
#include <stdio.h>
char *fgets (char *
string
, int
n
, FILE *
stream
);
Parameters
string
A pointer to a character array.
n
The maximum number of characters to read, plus one.
stream
A pointer to an open stream.
Return Values
x If successful, a pointer to a character array.
NULL An error occurred.
Description
The fgets function reads a string from an open stream. The
string
parameter is a pointer
to a character string, and
stream
is a file pointer to the input stream.
The fgets function reads
n
-1 characters or up to a newline character, whichever comes
first. If a newline character is encountered, that character is retained as part of the string.
(Contrast this with the gets function, which replaces the newline character with a null
character.)
The fgets function appends a null character to the string.
The function returns the pointer to the
string
argument if the read is successful. If an
end-of-file is encountered and no characters were read into the array, the contents of the
array remain unchanged and a null pointer is returned. A null pointer is also returned if
there is a read error. In this case, the contents of the array pointed to by
string
are
undefined.
Examples
The following program uses fgets() and fputs() to copy a file.
#include <stdio.h>
main(argc, argv)
int argc;
char *argv[ ];
{
char c, line[256], *fgets();
FILE *from, *to;