C file input/output
Template:Short description Template:Use dmy dates Template:C Standard Library 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 <templatestyles src="Mono/styles.css" /><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 <templatestyles src="Mono/styles.css" />std namespace).
| Byte character |
Wide character |
Description | |
|---|---|---|---|
| File access | Script error: No such module "anchor".fopen | Opens a file (with a non-Unicode filename on Windows and possible UTF-8 filename on Linux) | |
| Script error: No such module "anchor".popen | opens a process by creating a pipe, forking, and invoking the shell | ||
| Script error: No such module "anchor".freopen | Opens a different file with an existing stream | ||
| Script error: No such module "anchor".fflush | Synchronizes an output stream with the actual file | ||
| Script error: No such module "anchor".fclose | Closes a file | ||
| Script error: No such module "anchor".pclose | closes a stream | ||
| Script error: No such module "anchor".setbuf | Sets the buffer for a file stream | ||
| Script error: No such module "anchor".setvbuf | Sets the buffer and its size for a file stream | ||
| Script error: No such module "anchor".fwide | Switches a file stream between wide-character I/O and narrow-character I/O | ||
| Direct input/output |
Script error: No such module "anchor".fread | Reads from a file | |
| Script error: No such module "anchor".fwrite | Writes to a file | ||
| Unformatted input/output |
Script error: No such module "anchor".fgetc getc |
Script error: No such module "anchor".fgetwc getwc |
Reads a byte/<templatestyles src="Mono/styles.css" />wchar_t from a file stream |
| Script error: No such module "anchor".fgets | Script error: No such module "anchor".fgetws | Reads a byte/<templatestyles src="Mono/styles.css" />wchar_t line from a file stream | |
| Script error: No such module "anchor".fputc putc |
Script error: No such module "anchor".fputwc putwc |
Writes a byte/<templatestyles src="Mono/styles.css" />wchar_t to a file stream | |
| Script error: No such module "anchor".fputs | Script error: No such module "anchor".fputws | Writes a byte/<templatestyles src="Mono/styles.css" />wchar_t string to a file stream | |
| Script error: No such module "anchor".getchar | Script error: No such module "anchor".getwchar | Reads a byte/<templatestyles src="Mono/styles.css" />wchar_t from stdin | |
| Script error: No such module "anchor". |
— | Reads a byte string from stdin until a newline or end of file is encountered (deprecated in C99, removed from C11) | |
| Script error: No such module "anchor".putchar | Script error: No such module "anchor".putwchar | Writes a byte/<templatestyles src="Mono/styles.css" />wchar_t to stdout | |
| Script error: No such module "anchor".puts | — | Writes a byte string to stdout | |
| Script error: No such module "anchor".ungetc | Script error: No such module "anchor".ungetwc | Puts a byte/<templatestyles src="Mono/styles.css" />wchar_t back into a file stream | |
| Formatted input/output |
Script error: No such module "anchor".scanf fscanf sscanf |
Script error: No such module "anchor".wscanf fwscanf swscanf |
Reads formatted byte/<templatestyles src="Mono/styles.css" />wchar_t input from stdin, a file stream or a buffer |
| Script error: No such module "anchor".vscanf vfscanf vsscanf |
Script error: No such module "anchor".vwscanf vfwscanf vswscanf |
Reads formatted input byte/<templatestyles src="Mono/styles.css" />wchar_t from stdin, a file stream or a buffer using variable argument list | |
| Script error: No such module "anchor".printf fprintf sprintf snprintf |
Script error: No such module "anchor".wprintf fwprintf swprintf |
Prints formatted byte/<templatestyles src="Mono/styles.css" />wchar_t output to stdout, a file stream or a buffer | |
| Script error: No such module "anchor".vprintf vfprintf vsprintf vsnprintf |
Script error: No such module "anchor".vwprintf vfwprintf vswprintf |
Prints formatted byte/<templatestyles src="Mono/styles.css" />wchar_t output to stdout, a file stream, or a buffer using variable argument list | |
| Script error: No such module "anchor".perror | — | Writes a description of the current error to stderr | |
| File positioning | Script error: No such module "anchor".ftell ftello |
Returns the current file position indicator | |
| Script error: No such module "anchor".fseek fseeko |
Moves the file position indicator to a specific location in a file | ||
| Script error: No such module "anchor".fgetpos | Gets the file position indicator | ||
| Script error: No such module "anchor".fsetpos | Moves the file position indicator to a specific location in a file | ||
| Script error: No such module "anchor".rewind | Moves the file position indicator to the beginning in a file | ||
| Error handling |
Script error: No such module "anchor".clearerr | Clears errors | |
| Script error: No such module "anchor".feof | Checks for the end-of-file | ||
| Script error: No such module "anchor".ferror | Checks for a file error | ||
| Operations on files |
Script error: No such module "anchor".remove | Erases a file | |
| Script error: No such module "anchor".rename | Renames a file | ||
| Script error: No such module "anchor".tmpfile | Returns a pointer to a temporary file | ||
| Script error: No such module "anchor".tmpnam | Returns a unique filename | ||
Constants
Constants defined in the <stdio.h> header include:
| Name | Notes |
|---|---|
| EOF | A negative integer of type <templatestyles src="Mono/styles.css" />int used to indicate end-of-file conditions |
| Script error: No such module "anchor". BUFSIZ | An integer which is the size of the buffer used by the <templatestyles src="Mono/styles.css" />setbuf() function |
| FILENAME_MAX | The size of a <templatestyles src="Mono/styles.css" />char array which is large enough to store the name of any file that can be opened |
| FOPEN_MAX | The number of files that may be open simultaneously; will be at least eight |
| _IOFBF | An abbreviation for "input/output fully buffered"; it is an integer which may be passed to the <templatestyles src="Mono/styles.css" />setvbuf() function to request block buffered input and output for an open stream |
| _IOLBF | An abbreviation for "input/output line buffered"; it is an integer which may be passed to the <templatestyles src="Mono/styles.css" />setvbuf() function to request line buffered input and output for an open stream |
| _IONBF | An abbreviation for "input/output not buffered"; it is an integer which may be passed to the <templatestyles src="Mono/styles.css" />setvbuf() function to request unbuffered input and output for an open stream |
| L_tmpnam | The size of a <templatestyles src="Mono/styles.css" />char array which is large enough to store a temporary filename generated by the <templatestyles src="Mono/styles.css" />tmpnam() function |
| NULL | A 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_CUR | An integer which may be passed to the <templatestyles src="Mono/styles.css" />fseek() function to request positioning relative to the current file position |
| SEEK_END | An integer which may be passed to the <templatestyles src="Mono/styles.css" />fseek() function to request positioning relative to the end of the file |
| SEEK_SET | An integer which may be passed to the <templatestyles src="Mono/styles.css" />fseek() function to request positioning relative to the beginning of the file |
| TMP_MAX | The maximum number of unique filenames generable by the <templatestyles src="Mono/styles.css" />tmpnam() function; will be at least 25 |
Variables
Variables defined in the <stdio.h> header include:
| Name | Notes |
|---|---|
| stdin | A pointer to a <templatestyles src="Mono/styles.css" />FILE which refers to the standard input stream, usually a keyboard. |
| stdout | A pointer to a <templatestyles src="Mono/styles.css" />FILE which refers to the standard output stream, usually a display terminal. |
| stderr | A pointer to a <templatestyles src="Mono/styles.css" />FILE which refers to the standard error stream, often a display terminal. |
Member types
Data types defined in the <stdio.h> header include:
- <templatestyles src="Mono/styles.css" />FILE – also known as a Script error: No such module "anchor".file handle or a <templatestyles src="Template:Visible anchor/styles.css" /><span id="<templatestyles src="Mono/styles.css" />FILE pointer"><templatestyles src="Mono/styles.css" />FILE pointer, 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)
fpos_t– 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 encodingssize_t– an unsigned integer type which is the type of the result of the <templatestyles src="Mono/styles.css" />sizeof operator.
ExtensionsScript error: No such module "anchor".
The POSIX standard defines several extensions to <stdio.h> in its Base Definitions, among which are a readline function that allocates memory, the fileno and fdopen functions that establish the link between <templatestyles src="Mono/styles.css" />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 <templatestyles src="Mono/styles.css" />myfile.txt, reads five bytes from it, and then closes the file.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
const char FILE_NAME[] = "myfile.txt";
const int BUFFER_SIZE = 5;
char buffer[BUFFER_SIZE];
size_t len;
FILE* fp = fopen("myfile.txt", "rb");
if (!fp) {
fprintf(stderr, "Failed to open file \"%s\"!", FILE_NAME);
return EXIT_FAILURE;
}
if ((len = fread(buffer, 1, 5, fp)) < 0) {
fclose(fp);
fprintf(stderr, "An error occurred while reading file \"%s\".\n", FILE_NAME);
return EXIT_FAILURE;
}
fclose(fp);
printf("The bytes read were: ");
for (int i = 0; i < BUFFER_SIZE; ++i) {
printf("%02X ", buffer[i]);
}
putchar('\n');
return EXIT_SUCCESS;
}
Alternatives to stdioScript error: No such module "anchor".
Script error: No such module "redirect hatnote".
Several alternatives to <stdio.h> have been developed. Among these are C++ I/O headers <iostream> and <print>, part of the C++ Standard Library. ISO C++ still requires the <stdio.h> functionality, and it is found under header <cstdio>.
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.h>. 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
<templatestyles src="Reflist/styles.css" />
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ – Base Definitions Reference, The Single UNIX Specification, Version 5 from The Open Group
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
Script error: No such module "Check for unknown parameters".