String i/o

String i/o should only be used when working with text data, where newlines and null bytes have special meaning.

Input

The char *fgets(char *s, int size, FILE *stream); function reads a line of input into the buffer pointed at by s. It stops reading after the first newline character, at the end of the file, or once size-1 bytes have been read; a terminating null byte is stored after the last character in the buffer. The return value is s, or NULL if an error occurs, or if the end of input was reached before any characters were read.

The char *gets(char *s); function is similar, and reads from stdin. Unlike fgets, it does not have a length limit, and will read until the first newline or end of input–potentially overflowing the buffer pointed at by s. Additionally, when gets reaches a newline, it discards the newline in the returned string by replacing it with a null byte to terminate the string.

Warning

Because of the buffer overflow issue, gets is deprecated and unsafe.

Output

The char *fputs(char *s, FILE *stream); function writes the string pointed at by s to stream. As with fgets, the return value is s on success, or NULL on failure. The terminating null byte is not written.

The char *puts(char *s); function is similar, and writes to stdout. Unlike fputs, puts appends an extra newline after writing its string argument to stdout.