I/O Routines 输入/输出API函数

Input and Output

The I/O functions read and write data to and from files and devices. File I/O operations take place in text mode or binary mode. The Microsoft run-time library has three types of I/O functions:

  • Stream I/O functions treat data as a stream of individual characters.

  • Low-level I/O functions invoke the operating system directly for lower-level operation than that provided by stream I/O.

  • Console and port I/O functions read or write directly to a console (keyboard and screen) or an I/O port (such as a printer port).

Warning   Because stream functions are buffered and low-level functions are not, these two types of functions are generally incompatible. For processing a particular file, use either stream or low-level functions exclusively.

 

Low-level I/O

These functions invoke the operating system directly for lower-level operation than that provided by stream I/O. Low-level input and output calls do not buffer or format data.

这些函数直接访问操作系统进行低级别的操作,而不像流式输入输出。低级输入输出对数据不进行缓冲或格式化。

Low-level routines can access the standard streams opened at program startup using the following predefined handles:

StreamHandle
stdin0
stdout1
stderr2

Low-level I/O routines set the errno global variable when an error occurs. You must include STDIO.H when you use low-level functions only if your program requires a constant that is defined in STDIO.H, such as the end-of-file indicator (EOF).

Low-Level I/O Functions

FunctionUse
_closeClose file
_commitFlush file to disk
_creat, _wcreatCreate file
_dupReturn next available file handle for given file
_dup2Create second handle for given file
_eofTest for end of file
_lseek, _lseeki64Reposition file pointer to given location
_open, _wopenOpen file
_readRead data from file
_sopen, _wsopenOpen file for file sharing
_tell, _telli64Get current file-pointer position
_umaskSet file-permission mask
_writeWrite data to file

_dup and _dup2 are typically used to associate the predefined file handles with different files.

 

Stream I/O

These functions process data in different sizes and formats, from single characters to large data structures. They also provide buffering, which can improve performance. The default size of a stream buffer is 4K. These routines affect only buffers created by the run-time library routines, and have no effect on buffers created by the operating system.

Stream I/O Routines

RoutineUse
clearerrClear error indicator for stream
fcloseClose stream
_fcloseallClose all open streams except stdin, stdout, and stderr
_fdopen, wfdopenAssociate stream with handle to open file
feofTest for end of file on stream
ferrorTest for error on stream
fflushFlush stream to buffer or storage device
fgetc, fgetwcRead character from stream (function versions of getc and getwc)
_fgetchar, _fgetwcharRead character from stdin (function versions of getchar and getwchar)
fgetposGet position indicator of stream
fgets, fgetwsRead string from stream
_filenoGet file handle associated with stream
_flushallFlush all streams to buffer or storage device
fopen, _wfopenOpen stream
fprintf, fwprintfWrite formatted data to stream
fputc, fputwcWrite a character to a stream (function versions of putc and putwc)
_fputchar, _fputwcharWrite character to stdout (function versions of putchar and putwchar)
fputs, fputwsWrite string to stream
freadRead unformatted data from stream
freopen, _wfreopenReassign FILE stream pointer to new file or device
fscanf, fwscanfRead formatted data from stream
fseekMove file position to given location
fsetposSet position indicator of stream
_fsopen, _wfsopenOpen stream with file sharing
ftellGet current file position
fwriteWrite unformatted data items to stream
getc, getwcRead character from stream (macro versions of fgetc and fgetwc)
getchar, getwcharRead character from stdin (macro versions of fgetchar and fgetwchar)
gets, getwsRead line from stdin
_getwRead binary int from stream
printf, wprintfWrite formatted data to stdout
putc, putwcWrite character to a stream (macro versions of fputc and fputwc)
putchar, putwcharWrite character to stdout (macro versions of fputchar and fputwchar)
puts, _putwsWrite line to stream
_putwWrite binary int to stream
rewindMove file position to beginning of stream
_rmtmpRemove temporary files created by tmpfile
scanf, wscanfRead formatted data from stdin
setbufControl stream buffering
_setmaxstdioSet a maximum for the number of simultaneously open files at the stream I/O level.
setvbufControl stream buffering and buffer size
_snprintf, _snwprintfWrite formatted data of specified length to string
sprintf, swprintfWrite formatted data to string
sscanf, swscanfRead formatted data from string
_tempnam, _wtempnamGenerate temporary filename in given directory
tmpfileCreate temporary file
tmpnam, _wtmpnamGenerate temporary filename
ungetc, ungetwcPush character back onto stream
vfprintf, vfwprintf Write formatted data to stream
vprintf, vwprintfWrite formatted data to stdout
_vsnprintf, _vsnwprintfWrite formatted data of specified length to buffer
vsprintf, vswprintfWrite formatted data to buffer

When a program begins execution, the startup code automatically opens several streams: standard input (pointed to by stdin), standard output (pointed to by stdout), and standard error (pointed to by stderr). These streams are directed to the console (keyboard and screen) by default. Use freopen to redirect stdin, stdout, or stderr to a disk file or a device.

Files opened using the stream routines are buffered by default. The stdout and stderr functions are flushed whenever they are full or, if you are writing to a character device, after each library call. If a program terminates abnormally, output buffers may not be flushed, resulting in loss of data. Use fflush or _flushall to ensure that the buffer associated with a specified file or all open buffers are flushed to the operating system, which can cache data before writing it to disk. The commit-to-disk feature ensures that the flushed buffer contents are not lost in the event of a system failure.

There are two ways to commit buffer contents to disk:

  • Link with the file COMMODE.OBJ to set a global commit flag. The default setting of the global flag is n, for “no-commit.”

  • Set the mode flag to c with fopen or _fdopen.

Any file specifically opened with either the c or the n flag behaves according to the flag, regardless of the state of the global commit/no-commit flag.

If your program does not explicitly close a stream, the stream is automatically closed when the program terminates. However, you should close a stream when your program finishes with it, as the number of streams that can be open at one time is limited. See _setmaxstdio for information on this limit.

Input can follow output directly only with an intervening call to fflush or to a file-positioning function (fseek, fsetpos, or rewind). Output can follow input without an intervening call to a file-positioning function if the input operation encounters the end of the file.

以下摘自:http://www.linuxtopia.org/online_books/programming_books/gnu_libc_guide/Low_002dLevel-I_002fO.html

Stream-level I/O is more flexible and usually more convenient; therefore, programmers generally use the descriptor-level functions only when necessary. These are some of the usual reasons:

  • For reading binary files in large chunks.
  • For reading an entire file into core before parsing it.
  • To perform operations other than data transfer, which can only be done with a descriptor. (You can use fileno to get the descriptor corresponding to a stream.)
  • To pass descriptors to a child process. (The child can create its own stream to use a descriptor that it inherits, but cannot inherit a stream directly.)  
<script language="JavaScript" src="MS-ITS:dsmsdn.chm::/html/msdn_footer.js" type="text/javascript"></script> <script language="JavaScript" src="MS-ITS:dsmsdn.chm::/html/msdn_footer.js" type="text/javascript"></script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值