Английская Википедия:C file input/output

Материал из Онлайн справочника
Перейти к навигацииПерейти к поиску

Шаблон:Short description Шаблон:Use dmy dates Шаблон: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 Шаблон:Mono.[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 Шаблон:Mono (or in the C++ header Шаблон:Mono, which contains the standard C functionality but in the Шаблон:Mono namespace).

Byte
character
Wide
character
Description
File access Шаблон:Anchorfopen Opens a file (with a non-Unicode filename on Windows and possible UTF-8 filename on Linux)
Шаблон:Anchorfreopen Opens a different file with an existing stream
Шаблон:Anchorfflush Synchronizes an output stream with the actual file
Шаблон:Anchorfclose Closes a file
Шаблон:Anchorsetbuf Sets the buffer for a file stream
Шаблон:Anchorsetvbuf Sets the buffer and its size for a file stream
Шаблон:Anchorfwide Switches a file stream between wide-character I/O and narrow-character I/O
Direct
input/output
Шаблон:Anchorfread Reads from a file
Шаблон:Anchorfwrite Writes to a file
Unformatted
input/output
Шаблон:Anchorfgetc
getc
Шаблон:Anchorfgetwc
getwc
Reads a byte/Шаблон:Mono from a file stream
Шаблон:Anchorfgets Шаблон:Anchorfgetws Reads a byte/Шаблон:Mono line from a file stream
Шаблон:Anchorfputc
putc
Шаблон:Anchorfputwc
putwc
Writes a byte/Шаблон:Mono to a file stream
Шаблон:Anchorfputs Шаблон:Anchorfputws Writes a byte/Шаблон:Mono string to a file stream
Шаблон:Anchorgetchar Шаблон:Anchorgetwchar Reads a byte/Шаблон:Mono from stdin
Шаблон:Anchorgets Шаблон:N/a Reads a byte string from stdin until a newline or end of file is encountered (deprecated in C99, removed from C11)
Шаблон:Anchorputchar Шаблон:Anchorputwchar Writes a byte/Шаблон:Mono to stdout
Шаблон:Anchorputs Шаблон:N/a Writes a byte string to stdout
Шаблон:Anchorungetc Шаблон:Anchorungetwc Puts a byte/Шаблон:Mono back into a file stream
Formatted
input/output
Шаблон:Anchorscanf
fscanf
sscanf
Шаблон:Anchorwscanf
fwscanf
swscanf
Reads formatted byte/Шаблон:Mono input from stdin,
a file stream or a buffer
Шаблон:Anchorvscanf
vfscanf
vsscanf
Шаблон:Anchorvwscanf
vfwscanf
vswscanf
Reads formatted input byte/Шаблон:Mono from stdin,
a file stream or a buffer using variable argument list
Шаблон:Anchorprintf
fprintf
sprintf
snprintf
Шаблон:Anchorwprintf
fwprintf
swprintf
Prints formatted byte/Шаблон:Mono output to stdout,
a file stream or a buffer
Шаблон:Anchorvprintf
vfprintf
vsprintf
vsnprintf
Шаблон:Anchorvwprintf
vfwprintf
vswprintf
Prints formatted byte/Шаблон:Mono output to stdout,
a file stream, or a buffer using variable argument list
Шаблон:Anchorperror Шаблон:N/a Writes a description of the current error to stderr
File positioning Шаблон:Anchorftell
ftello
Returns the current file position indicator
Шаблон:Anchorfseek
fseeko
Moves the file position indicator to a specific location in a file
Шаблон:Anchorfgetpos Gets the file position indicator
Шаблон:Anchorfsetpos Moves the file position indicator to a specific location in a file
Шаблон:Anchorrewind Moves the file position indicator to the beginning in a file
Error
handling
Шаблон:Anchorclearerr Clears errors
Шаблон:Anchorfeof Checks for the end-of-file
Шаблон:Anchorferror Checks for a file error
Operations
on files
Шаблон:Anchorremove Erases a file
Шаблон:Anchorrename Renames a file
Шаблон:Anchortmpfile Returns a pointer to a temporary file
Шаблон:Anchortmpnam Returns a unique filename

Constants

Constants defined in the Шаблон:Mono header include:

Name Notes
EOF A negative integer of type Шаблон:Mono used to indicate end-of-file conditions
Шаблон:Anchor BUFSIZ An integer which is the size of the buffer used by the Шаблон:Mono function
FILENAME_MAX The size of a Шаблон:Mono 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 Шаблон:Mono 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 Шаблон:Mono 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 Шаблон:Mono function to request unbuffered input and output for an open stream
L_tmpnam The size of a Шаблон:Mono array which is large enough to store a temporary filename generated by the Шаблон:Mono 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 Шаблон:Mono function to request positioning relative to the current file position
SEEK_END An integer which may be passed to the Шаблон:Mono function to request positioning relative to the end of the file
SEEK_SET An integer which may be passed to the Шаблон:Mono function to request positioning relative to the beginning of the file
TMP_MAX The maximum number of unique filenames generable by the Шаблон:Mono function; will be at least 25

Variables

Файл:Stdstreams-notitle.svg

Variables defined in the Шаблон:Mono header include:

Name Notes
stdin A pointer to a Шаблон:Mono which refers to the standard input stream, usually a keyboard.
stdout A pointer to a Шаблон:Mono which refers to the standard output stream, usually a display terminal.
stderr A pointer to a Шаблон:Mono which refers to the standard error stream, often a display terminal.

Member types

Data types defined in the Шаблон:Mono header include:

  • Шаблон:Mono – also known as a Шаблон:Anchorfile handle or a Шаблон:Visible anchor, 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)
  • Шаблон:Mono – 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
  • Шаблон:Mono – an unsigned integer type which is the type of the result of the Шаблон:Mono operator.

ExtensionsШаблон:Anchor

The POSIX standard defines several extensions to Шаблон:Mono in its Base Definitions, among which are a Шаблон:Mono function that allocates memory, the Шаблон:Mono and Шаблон:Mono functions that establish the link between Шаблон:Mono objects and file descriptors, and a group of functions for creating Шаблон:Mono 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Шаблон:Anchor

Шаблон:Redirect

Several alternatives to Шаблон:Mono have been developed. Among these is the [[Input/output (C++)|C++ Шаблон:Mono]] library, part of the ISO C++ standard. ISO C++ still requires the Шаблон:Mono 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 Шаблон:Mono. 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

Шаблон:Reflist

External links

Шаблон:Wikibooks

Шаблон:CProLang