C file input/output

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 . The functionality descends from a "portable I/O package" written by Mike Lesk at Bell Labs in the early 1970s, and officially became part of the Unix operating system in Version 7.

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.

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).

Constants
Constants defined in the  header include:

Variables

Variables defined in the  header include:

Member types
Data types defined in the  header include:
 – also known as a file handle or a , this is an opaque pointer containing the information about a file or text stream needed to perform input or output operations on it, including:
platform-specific identifier of the associated I/O device, such as a file descriptor 
the buffer
stream orientation indicator (unset, narrow, or wide)
stream buffering state indicator (unbuffered, line buffered, fully buffered)
I/O mode indicator (input stream, output stream, or update stream)
binary/text mode indicator
end-of-file indicator
error indicator
the current stream position and multibyte conversion state (an object of type mbstate_t)
reentrant lock (required as of C11)
 – a non-array type capable of uniquely identifying the position of every byte in a file and every conversion state that can occur in all supported multibyte character encodings
 – an unsigned integer type which is the type of the result of the  operator.

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.

#include <stdio.h>
#include <stdlib.h>

int main(void) {
    char buffer[5];
    FILE* fp = fopen("myfile", "rb");

    if (fp == NULL) {
        perror("Failed to open file \"myfile\"");
        return EXIT_FAILURE;
    }

    for (int i = 0; i < 5; i++) {
        int rc = getc(fp);
        if (rc == EOF) {
            fputs("An error occurred while reading the file.\n", stderr);
            return EXIT_FAILURE;
        }
        
        buffer[i] = rc;
    }

    fclose(fp);

    printf("The bytes read were... %02x %02x %02x %02x %02x\n", buffer[0], buffer[1],
        buffer[2], buffer[3], buffer[4]);

    return EXIT_SUCCESS;
}

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 (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. It was released to the outside world in 1997, and the last release was 1 February 2005.

See also
printf format string
scanf format string

References

External links

C standard library
Input/output
Articles with example C code