get a string of characters from a file
#include <stdio.h> char *gets( char *buf );
The gets() function gets a string of characters from the file designated by stdin, and stores them in the array pointed to by buf until end-of-file is encountered or a newline character is read. Any newline character is discarded, and a null character is placed immediately after the last character read into the array.
![]() |
It's better to use fgets() than gets(). With gets(), data beyond the array buf is destroyed if a newline character isn't read from the input stream stdin before the end of the array buf is reached. |
The gets() function returns buf if successful. NULL is returned if end-of-file is encountered, or if a read error occurs. When an error has occurred, errno contains a value that indicates the type of error that has been detected.
#include <stdio.h>
void main()
{
char buffer[80];
while( gets( buffer ) != NULL )
puts( buffer );
}
ANSI
| Safety: | |
|---|---|
| Interrupt handler | No |
| Signal handler | No |
| Thread | Yes |
errno, fgetc(), fgetchar(), fgets(), fopen(), getc(), getchar(), ungetc()