C file input/output explained

The C programming language provides many standard library functions for file input and output. These functions make up the bulk of the C standard library header .[1] The functionality descends from a "portable I/O package" written by Mike Lesk at Bell Labs in the early 1970s,[2] and officially became part of the Unix operating system in Version 7.[3]

The I/O functionality of C is fairly low-level by modern standards; C abstracts all file operations into operations on streams of bytes, which may be "input streams" or "output streams". Unlike some earlier programming languages, C has no direct support for random-access data files; to read from a record in the middle of a file, the programmer must create a stream, seek to the middle of the file, and then read bytes in sequence from the stream.

The stream model of file I/O was popularized by Unix, which was developed concurrently with the C programming language itself. The vast majority of modern operating systems have inherited streams from Unix, and many languages in the C programming language family have inherited C's file I/O interface with few if any changes (for example, PHP).

Overview

This library uses what are called streams to operate with physical devices such as keyboards, printers, terminals or with any other type of files supported by the system. Streams are an abstraction to interact with these in a uniform way. All streams have similar properties independent of the individual characteristics of the physical media they are associated with.[4]

Functions

Most of the C file input/output functions are defined in (or in the C++ header, which contains the standard C functionality but in the namespace).

Byte
character
Wide
character
Description
File accessfopenOpens a file (with a non-Unicode filename on Windows and possible UTF-8 filename on Linux)
freopenOpens a different file with an existing stream
fflushSynchronizes an output stream with the actual file
fcloseCloses a file
setbufSets the buffer for a file stream
setvbufSets the buffer and its size for a file stream
fwideSwitches a file stream between wide-character I/O and narrow-character I/O
Direct
input/output
freadReads from a file
fwriteWrites to a file
Unformatted
input/output
fgetc
getc
fgetwc
getwc
Reads a byte/ from a file stream
fgetsfgetwsReads a byte/ line from a file stream
fputc
putc
fputwc
putwc
Writes a byte/ to a file stream
fputsfputwsWrites a byte/ string to a file stream
getchargetwcharReads a byte/ from stdin
getsReads a byte string from stdin until a newline or end of file is encountered (deprecated in C99, removed from C11)
putcharputwcharWrites a byte/ to stdout
putsWrites a byte string to stdout
ungetcungetwcPuts a byte/ back into a file stream
Formatted
input/output
scanf
fscanf
sscanf
wscanf
fwscanf
swscanf
Reads formatted byte/ input from stdin,
a file stream or a buffer
vscanf
vfscanf
vsscanf
vwscanf
vfwscanf
vswscanf
Reads formatted input byte/ from stdin,
a file stream or a buffer using variable argument list
printf
fprintf
sprintf
snprintf
wprintf
fwprintf
swprintf
Prints formatted byte/ output to stdout,
a file stream or a buffer
vprintf
vfprintf
vsprintf
vsnprintf
vwprintf
vfwprintf
vswprintf
Prints formatted byte/ output to stdout,
a file stream, or a buffer using variable argument list
perrorWrites a description of the current error to stderr
File positioningftell
ftello
Returns the current file position indicator
fseek
fseeko
Moves the file position indicator to a specific location in a file
fgetposGets the file position indicator
fsetposMoves the file position indicator to a specific location in a file
rewindMoves the file position indicator to the beginning in a file
Error
handling
clearerrClears errors
feofChecks for the end-of-file
ferrorChecks for a file error
Operations
on files
removeErases a file
renameRenames a file
tmpfileReturns a pointer to a temporary file
tmpnamReturns a unique filename

Constants

Constants defined in the header include:

Name Notes
EOFA negative integer of type used to indicate end-of-file conditions
BUFSIZAn integer which is the size of the buffer used by the function
FILENAME_MAXThe size of a array which is large enough to store the name of any file that can be opened
FOPEN_MAXThe number of files that may be open simultaneously; will be at least eight
_IOFBFAn abbreviation for "input/output fully buffered"; it is an integer which may be passed to the function to request block buffered input and output for an open stream
_IOLBFAn abbreviation for "input/output line buffered"; it is an integer which may be passed to the function to request line buffered input and output for an open stream
_IONBFAn abbreviation for "input/output not buffered"; it is an integer which may be passed to the function to request unbuffered input and output for an open stream
L_tmpnamThe size of a array which is large enough to store a temporary filename generated by the function
NULLA macro expanding to the null pointer constant; that is, a constant representing a pointer value which is guaranteed not to be a valid address of an object in memory
SEEK_CURAn integer which may be passed to the function to request positioning relative to the current file position
SEEK_ENDAn integer which may be passed to the function to request positioning relative to the end of the file
SEEK_SETAn integer which may be passed to the function to request positioning relative to the beginning of the file
TMP_MAXThe maximum number of unique filenames generable by the function; will be at least 25

Variables

Variables defined in the header include:

Name Notes
stdinA pointer to a which refers to the standard input stream, usually a keyboard.
stdoutA pointer to a which refers to the standard output stream, usually a display terminal.
stderrA pointer to a which refers to the standard error stream, often a display terminal.

Member types

Data types defined in the header include:

Extensions

The POSIX standard defines several extensions to in its Base Definitions, among which are a function that allocates memory, the and functions that establish the link between objects and file descriptors, and a group of functions for creating objects that refer to in-memory buffers.

Example

The following C program opens a binary file called myfile, reads five bytes from it, and then closes the file.

  1. include
  2. include

int main(void)

Alternatives to stdio

Several alternatives to have been developed. Among these is the C++ library, part of the ISO C++ standard. ISO C++ still requires the functionality.

Other alternatives include the Sfio[5] (A Safe/Fast I/O Library) library from AT&T Bell Laboratories. This library, introduced in 1991, aimed to avoid inconsistencies, unsafe practices and inefficiencies in the design of . Among its features is the possibility to insert callback functions into a stream to customize the handling of data read from or written to the stream.[6] It was released to the outside world in 1997, and the last release was 1 February 2005.[7]

See also

Notes and References

  1. Book: ISO/IEC 9899:1999 specification . p. 274, § 7.19 . en-US.
  2. Book: Kernighan . Brian . Brian Kernighan . Pike . Rob . Rob Pike . . . . 1984 . 200.
  3. M. D. . McIlroy . Doug McIlroy . 1987 . A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986 . CSTR . 139 . Bell Labs.
  4. Web site: (stdio.h) - C++ Reference . July 25, 2021 . C++ . en-US.
  5. Web site: Sfio: A Safe/Fast I/O Library . 16 March 2021 . 11 February 2006 . https://web.archive.org/web/20060211021834/http://akpublic.research.att.com/sw/tools/sfio/ . bot: unknown .
  6. SFIO: Safe/Fast String/File IO . David G. . Korn . David Korn (computer scientist) . Kiem-Phong . Vo . Proc. Summer USENIX Conf. . 1991 . 10.1.1.51.6574.
  7. Glenn S. . Fowler . David G. . Korn . Kiem-Phong . Vo . Extended Formatting with Sfio . Proc. Summer USENIX Conf. . 2000.