NAME
fgetc, getc, getchar, fputc, putc, putchar, ungetc, fgets, gets, fputs, puts, fread, fwrite – Stdio input and output

SYNOPSIS
#include <u.h>
#include <stdio.h>

int    fgetc(FILE *f)

int    getc(FILE *f)

int    getchar(void)

int    fputc(int c, FILE *f)

int    putc(int c, FILE *f)

int    putchar(int c)

int    ungetc(int c, FILE *f)

char *fgets(char *s, int n, FILE *f)

char *gets(char *s)

int    fputs(char *s, FILE *f)

int    puts(char *s)

long fread(void *ptr, long itemsize, long nitems, FILE *stream)

long fwrite(void *ptr, long itemsize, long nitems, FILE *stream)

DESCRIPTION
The functions described here work on open Stdio streams (see fopen).

Fgetc returns as an int the next unsigned char from input stream f. If the stream is at end–of–file, the end–of–file indicator for the stream is set and fgetc returns EOF. If a read error occurs, the error indicator for the stream is set and fgetc returns EOF. Getc is like fgetc except that it is implemented as a macro. Getchar is like getc except that it always reads from stdin.

Ungetc pushes character c back onto the input stream f. The pushed–back character will be returned by subsequent reads in the reverse order of their pushing. A successful intervening fseek, fsetpos, or rewind on f discards any pushed–back characters for f. One character of push–back is guaranteed. Ungetc returns the character pushed back (converted to unsigned char), or EOF if the operation fails. A successful call to ungetc clears the end–of–file indicator for the stream. The file position indicator for the stream after reading or discarding all pushed–back characters is the same as it was before the characters were pushed back.

Fputc writes character c (converted to unsigned char) to output stream f at the position indicated by the position indicator for the stream and advances the indicator appropriately. If the file cannot support positioning requests, or if the stream was opened with append mode, the character is appended to the output stream. Fputc returns the character written or EOF if there was a write error. Putc is like fputc but is implemented as a macro. Putchar is like putc except that it always writes to stdout.

All other input takes place as if characters were read by successive calls to fgetc and all other output takes place as if characters were written by successive calls to fputc.

Fgets reads up to and including the next newline, but not past end–of–file or more than n–1 characters, from stream f into array s. A null character is written immediately after the last character read into the array (if any characters are read at all). Fgets returns s if successful, otherwise a null pointer. Gets is similar to fgets except that it always reads from stdin and it discards the terminating newline, if any. Gets does not check for overflow of the receiving array, so its use is deprecated.

Fputs writes the string s to stream f, returning EOF if a write error occurred, otherwise a nonnegative value. The terminating null character is not written. Puts is the same, writing to stdout.

Fread reads from the named input stream at most nitems of data of size itemsize and the type of *ptr into a block beginning at ptr. It returns the number of items actually read.

Fwrite appends to the named output stream at most nitems of data of size itemsize and the type of *ptr from a block beginning at ptr. It returns the number of items actually written.

SOURCE
/sys/src/libstdio

SEE ALSO
read(2), fopen(2), bio(2)

BUGS
Stdio does not handle UTF or runes; use Bio instead.
Copyright © 2024 Plan 9 Foundation. All rights reserved.