stringtranslate.com

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 <stdio.h>.[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 <stdio.h> (or in the C++ header cstdio, which contains the standard C functionality but in the std namespace).

Constants

Constants defined in the <stdio.h> header include:

Variables

Variables defined in the <stdio.h> header include:

Member types

Data types defined in the <stdio.h> header include:

Extensions

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

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; } if (fread(buffer, 1, 5, fp) < 5) { fputs("An error occurred while reading the file.\n", stderr); return EXIT_FAILURE; } fclose(fp); printf("The bytes read were: "); for (int i = 0; i < 5; ++i) { printf("%02X ", buffer[i]); } putchar('\n'); return EXIT_SUCCESS;}

Alternatives to stdio

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

Other alternatives include the Sfio[6] (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 stdio. 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.[7] It was released to the outside world in 1997, and the last release was 1 February 2005.[8]

See also

References

  1. ^ ISO/IEC 9899:1999 specification. p. 274, § 7.19.
  2. ^ Kernighan, Brian; Pike, Rob (1984). The UNIX Programming Environment. Englewood Cliffs: Prentice Hall. p. 200.
  3. ^ McIlroy, M. D. (1987). A Research Unix reader: annotated excerpts from the Programmer's Manual, 1971–1986 (PDF) (Technical report). CSTR. Bell Labs. 139.
  4. ^ "(stdio.h) - C++ Reference". C++. Retrieved 25 July 2021.
  5. ^ stdio.h – Base Definitions Reference, The Single UNIX Specification, Version 4 from The Open Group
  6. ^ "Sfio: A Safe/Fast I/O Library". Archived from the original on 11 February 2006. Retrieved 16 March 2021.{{cite web}}: CS1 maint: bot: original URL status unknown (link)
  7. ^ Korn, David G.; Vo, Kiem-Phong (1991). SFIO: Safe/Fast String/File IO. Proc. Summer USENIX Conf. CiteSeerX 10.1.1.51.6574.
  8. ^ Fowler, Glenn S.; Korn, David G.; Vo, Kiem-Phong (2000). Extended Formatting with Sfio. Proc. Summer USENIX Conf.

External links