fgets [ HP C/iX Library Reference Manual ] MPE/iX 5.0 Documentation
HP C/iX Library Reference Manual
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;
if(argc != 3) {
printf("Usage: cp fromfile tofile\n");
exit(1);
}
from = fopen(argv[1], "r");
if(from == NULL) {
printf("Can't open %s.\n", argv[1]);
exit(1);
}
to = fopen(argv[2], "w");
if(to == NULL) {
printf("Can't create %s.\n", argv[2]);
exit(1);
}
while(fgets(line, 256, from) != NULL)
fputs(line, to);
exit(0);
}
The program above accepts two arguments: the first is the name of the
file to be copied, and the second is the name of the file to be created.
The first file is opened for reading, and the second file is created for
writing. The data from the first file is copied directly to the newly
created file.
In this program, the return value of fgets() is compared to NULL in the
while loop, because fgets() returns the null pointer when it reaches the
end of its input. You can easily convert this program to a file print
command by doing the following:
1. Change the argc comparison to
if(argc != 2) . . .
2. Remove the to file pointer.
3. Remove the block of code that uses fopen() to open the new file,
and assign a value to to.
4. Change the fputs() call to
fputs(line, stdout);
The new file print program should look like this:
#include <stdio.h>
main(argc, argv)
int argc;
char *argv[ ];
{
char c, line[256], *fgets();
FILE *from;
if(argc < 2) {
printf("Usage: cat file\n");
exit(1);
}
from = fopen(argv[1], "r");
if(from == NULL) {
printf("Can't open %s.\n", argv[1]);
exit(1);
}
while(fgets(line, 256, from) != NULL)
fputs(line, stdout);
exit(0);
}
See Also
ferror(), fopen(), fread(), getc(), puts(), scanf() ANSI C 4.9.7.2,
POSIX.1 8.1
MPE/iX 5.0 Documentation