C file input/output
-
C Standard Library - Data types
- Character classification
- Strings
- Mathematics
- File input/output
- Date/time
- Localization
- Memory allocation
- Program control
- Miscellaneous headers:
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]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 the Unix operating system, 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). The C++ standard library reflects the "stream" concept in its syntax; see iostream.
Contents
Overview of functions
Most of the C file input/output functions are defined in
stdio.h
(cstdio
header in C++).- File access
fopen
- opens a filefreopen
- opens a different file with an existing streamfflush
- synchronizes an output stream with the actual filefclose
- closes a filesetbuf
- sets the buffer for a file streamsetvbuf
- sets the buffer and its size for a file streamfwide
- switches a file stream between wide character I/O and narrow character I/O
- Direct input/output
- Unformatted input/output
-
- Narrow character
fgetc
,getc
- reads a character from a file streamfgets
- reads a character string from a file streamfputc
,putc
- writes a character to a file streamfputs
- writes a character string to a file streamgetchar
- reads a character from stdingets
- reads a character string from stdinputchar
- writes a character to stdoutputs
- writes a character string to stdoutungetc
- puts a character back into a file stream
-
- Wide character
fgetwc
,getwc
- reads a wide character from a file streamfgetws
- reads a wide character string from a file streamfputwc
,putwc
- writes a wide character to a file streamfputws
- writes a wide character string to a file streamgetwchar
- reads a wide character from stdinputwchar
- writes a wide character to stdoutungetwc
- puts a wide character back into a file stream
- Formatted input/output
-
- Narrow character
scanf
,fscanf
,sscanf
- reads formatted input from stdin, a file stream or a buffervscanf
,vfscanf
,wsscanf
- reads formatted input from stdin, a file stream or a buffer using variable argument listprintf
,fprintf
,sprintf
,snprintf
prints formatted output to stdout, a file stream or a buffervprintf
,vfprintf
,vsprintf
,vsnprintf
- prints formatted output to stdout, a file stream, or a buffer using variable argument list
-
- Wide character
wscanf
,fwscanf
,swscanf
- reads formatted wide character input from stdin, a file stream or a buffervwscanf
,vfwscanf
,vswscanf
- reads formatted wide character input from stdin, a file stream or a buffer using variable argument listwprintf
,fwprintf
,swprintf
- prints formatted wide character output to stdout, a file stream or a buffervwprintf
,vfwprintf
,vswprintf
- prints formatted wide character output to stdout, a file sttream or a buffer using variable argument list
- File positioning
ftell
- returns the current file position indicatorfgetpos
- gets the file position indicatorfseek
- moves the file position indicator to a specific location in a filefsetpos
- moves the file position indicator to a specific location in a filerewind
- moves the file position indicator to the beginning in a file
- Error handling
clearerr
- clears errorsfeof
- checks for the end-of-fileferror
- checks for a file errorperror
- displays a character string corresponding of the current error to stderr
- Operations on files
remove
- erases a filerename
- renames a filetmpfile
- returns a pointer to a temporary filetmpnam
- returns a unique filename
Constants
Constants defined in the
stdio.h
header include:Name Notes EOF
a negative integer of type int
used to indicate end-of-file conditionsBUFSIZ
an integer which is the size of the buffer used by the setbuf()
functionFILENAME_MAX
the size of a char
array which is large enough to store the name of any file that can be openedFOPEN_MAX
the number of files that may be open simultaneously; will be at least 8 _IOFBF
an abbreviation for "input/output fully buffered"; it is an integer which may be passed to the 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 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 setvbuf()
function to request unbuffered input and output for an open streamL_tmpnam
the size of a char
array which is large enough to store a temporary filename generated by thetmpnam()
functionNULL
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 fseek()
function to request positioning relative to the current file positionSEEK_END
an integer which may be passed to the fseek()
function to request positioning relative to the end of the fileSEEK_SET
an integer which may be passed to the fseek()
function to request positioning relative to the beginning of the fileTMP_MAX
the maximum number of unique filenames generable by the tmpnam()
function; will be at least 25Variables
Variables defined in the
stdio.h
header include:Name Notes stdin
a pointer to a FILE
which refers to the standard input stream, usually a keyboard.stdout
a pointer to a FILE
which refers to the standard output stream, usually a display terminal.stderr
a pointer to a FILE
which refers to the standard error stream, often a display terminal.Member types
Data types defined in the
stdio.h
header include:FILE
- a structure containing the information about a file or text stream needed to perform input or output operations on it, including:- a file descriptor
- the current stream position
- an end-of-file indicator
- an error indicator
- a pointer to the stream's buffer, if applicable
fpos_t
- a non-array type capable of uniquely identifying the position of every byte in a file.size_t
- an unsigned integer type which is the type of the result of thesizeof
operator.
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] = {0}; /* initialized to zeroes */ int i, rc; FILE *fp = fopen("myfile", "rb"); if (fp == NULL) { perror("Failed to open file \"myfile\""); return EXIT_FAILURE; } for (i = 0; (rc = getc(fp)) != EOF && i < 5; buffer[i++] = rc) ; fclose(fp); if (i == 5) { puts("The bytes read were..."); printf("%x %x %x %x %x\n", buffer[0], buffer[1], buffer[2], buffer[3], buffer[4]); } else fputs("There was an error reading the file.\n", stderr); return EXIT_SUCCESS; }
References
- ^ ISO/IEC 9899:1999 specification. p. 274, § 7.19. http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf.
- ^ Kernighan, Brian; Rob Pike (1984). The UNIX Programming Environment. Englewood Cliffs: Prentice Hall. p. 200.
External links
Categories:
Wikimedia Foundation. 2010.
Look at other dictionaries:
Input/output — In computing, input/output, or I/O, refers to the communication between an information processing system (such as a computer), and the outside world – possibly a human, or another information processing system. Inputs are the signals or data… … Wikipedia
Input/output completion port — IOCP redirects here. For the program on IBM mainframes, see Input/Output Configuration Program. Input/Output Completion Port (IOCP) is an API for performing multiple simultaneous asynchronous input/output operations in Windows NT versions 3.5 and … Wikipedia
Audio Stream Input/Output — (ASIO) is a computer soundcard driver protocol for digital audio specified by Steinberg, providing a low latency and high fidelity interface between a software application and a computer s sound card. Whereas Microsoft’s DirectSound is commonly… … Wikipedia
Input — is the term denoting either an entrance or changes which are inserted into a system and which activate/modify a process. It is an abstract concept, used in the modeling, system(s) design and system(s) exploitation. It is usually connected with… … Wikipedia
File Allocation Table — For other uses, see Fat (disambiguation). FAT Developer Microsoft Full Name File Allocation Table FAT12 (12‑bit version) FAT16/FAT16B (16‑bit versions) FAT32 (32‑bit version with 28 bits used) Introduced … Wikipedia
File dialog — A save as file dialog from GTK+. The file browser is hidden inside a disclosure widget. In computing, a file dialog (also called File Selector/Chooser, file requester, or open and save dialog) is a dialog box that allows users to choose a file… … Wikipedia
file — file1 fileable, adj. filer, n. /fuyl/, n., v., filed, filing. n. 1. a folder, cabinet, or other container in which papers, letters, etc., are arranged in convenient order for storage or reference. 2. a collection of papers, records, etc.,… … Universalium
input — in|put1 W3 [ˈınput] n 1.) [U] information that is put into a computer ≠ ↑output ▪ If the input data specified it, the file will close and the process terminates. 2.) [U and C] ideas, advice, money, or effort that you put into a job or activity in … Dictionary of contemporary English
input — noun 1 of time/knowledge/ideas ADJECTIVE ▪ great, important, major, significant, substantial ▪ additional ▪ direct ▪ … Collocations dictionary
file — I. noun Etymology: Middle English, from Old English fēol; akin to Old High German fīla file Date: before 12th century 1. a tool usually of hardened steel with cutting ridges for forming or smoothing surfaces especially of metal 2. a shrewd or… … New Collegiate Dictionary