Cstdio
C library to perform Input/Output operations.
Input and Output operations can also be performed in C++ using the C Standard Input and Output Library (cstdio, known as stdio.h in the C language). 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 an uniform way; All streams have similar properties independently of the individual characteristics of the physical media they are associated with.
Streams are handled in the cstdio library as pointers to FILE objects. A pointer to a FILE object uniquely identifies a stream, and is used as a parameter in the operations involving that stream.
There also exist three standard streams: stdin, stdout and stderr, which are automatically created and opened for all programs using the library.
Stream properties
Streams have some properties that define which functions can be used on them and how these will treat the data input or output through them. Most of these properties are defined at the moment the stream is associated with a file (opened) using the fopen function:
- Read/Write Access
- Specifies whether the stream has read or write access (or both) to the physical media they are associated with.
- Text/Binary
- Text streams are thought to represent a set of text lines, each one ending with a new-line character. Depending on the environment where the application is run some character translation may occur with text streams to adapt some special characters to the text file specifications of the environment. A binary stream, on the other hand, is a sequence of characters written or read from the physical media with no translation, having a one-to-one correspondence with the characters read or written to the stream.
- Buffer
- A buffer is a block of memory where data is accumulated before being physically read or written to the associated file or device. Streams can be either fully buffered, line buffered or unbuffered. On fully buffered streams, data is read/written when the buffer is filled, on line buffered streams this happens when a new-line character is encountered, and on unbuffered streams characters are intended to be read/written as soon as possible.
Indicators
Streams have certain internal indicators that specify their current state and which affect the behavior of some input and output operations performed on them:
- Error indicator
- This indicator is set when an error has occurred in an operation related to the stream. This indicator can be checked with the ferror function, and can be reset by calling either to clearerr or to any repositioning function (rewind, fseek and fsetpos).
- End-Of-File indicator
- When set, indicates that the last reading or writing operation performed with the stream reached the End of File. It can be checked with the feof function, and can be reset by calling either to clearerr or to any repositioning function (rewind, fseek and fsetpos).
- Position indicator
- It is an internal pointer of each stream which points to the next character to be read or written in the next I/O operation. Its value can be obtained by the ftell and fgetpos functions, and can be changed using the repositioning functions rewind, fseek and fsetpos.
Functions
Formatted input/output
fprintf | Write formatted data to stream (function) |
fscanf | Read formatted data from stream (function) |
Print formatted data to stdout (function) | |
scanf | Read formatted data from stdin (function) |
snprintf | Write formatted output to sized buffer (function) |
sprintf | Write formatted data to string (function) |
sscanf | Read formatted data from string (function) |
vfprintf | Write formatted data from variable argument list to stream (function) |
vfscanf | Read formatted data from stream into variable argument list (function) |
vprintf | Print formatted data from variable argument list to stdout (function) |
vscanf | Read formatted data into variable argument list (function) |
vsnprintf | Write formatted data from variable argument list to sized buffer (function) |
vsprintf | Write formatted data from variable argument list to string (function) |
vsscanf | Read formatted data from string into variable argument list (function) |
See also
- tmpnam
Favorite site
- Wikipedia (en) C file input/output
- 입출력함수.Stream I/O(fopen, freopen, _fdopen, fclose, _fcloseall, fread, fwrite, fflush, feof, fgetc, fputc, fgets, fputs, fseek, ftell, fprintf, fscanf) 1
References
-
Blog.naver.com_-pointer98-_io_file_functions.pdf ↩