Guardian C Library Calls Reference Manual
fgets
3-44 128833—Guardian TNS C Library Calls Reference Manual
Reference to Library Calls
fgets
The fgets function reads a string from a file opened for ANSI I/O.
str_ptr
points to the string where fgets stores the input.
str_size
specifies the size in bytes of *str_ptr. fgets reads up to str_size - 1 characters.
stream
denotes a file opened for ANSI I/O.
Return Value
is str_ptr if the operation is successful. If fgets encounters the end of the file or an
error, it returns the pointer value NULL. To distinguish between these two cases, use
the feof or ferror function.
Usage Guidelines
•
The fgets function reads from a file until it encounters a newline character or the end
of the file or until it has read str_size - 1 characters. If it encounters a newline
character, fgets copies it into *str_ptr. Following the input, fgets stores a null
character into the byte following the last character read.
Example
This example reads a string from the file $a.b.c and writes it to the array named sp:
#include <stdioh>
#define SPSIZE 200
FILE *fp;
char sp[SPSIZE];
char *cp;
fp = fopen("$a.b.c", "r");
/* ... */
cp = fgets(sp, SPSIZE, fp);
#include <stdioh>
char *fgets(char *str_ptr, int str_size, FILE *stream);