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

来源:互联网 发布:java 连接 pi数据库 编辑:程序博客网 时间:2024/05/23 00:08

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:

Stream Handle stdin 0 stdout 1 stderr 2

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

Function Use _close Close file _commit Flush file to disk _creat, _wcreat Create file _dup Return next available file handle for given file _dup2 Create second handle for given file _eof Test for end of file _lseek, _lseeki64 Reposition file pointer to given location _open, _wopen Open file _read Read data from file _sopen, _wsopen Open file for file sharing _tell, _telli64 Get current file-pointer position _umask Set file-permission mask _write Write 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

Routine Use clearerr Clear error indicator for stream fclose Close stream _fcloseall Close all open streams except stdin, stdout, and stderr _fdopen, wfdopen Associate stream with handle to open file feof Test for end of file on stream ferror Test for error on stream fflush Flush stream to buffer or storage device fgetc, fgetwc Read character from stream (function versions of getc and getwc) _fgetchar, _fgetwchar Read character from stdin (function versions of getchar and getwchar) fgetpos Get position indicator of stream fgets, fgetws Read string from stream _fileno Get file handle associated with stream _flushall Flush all streams to buffer or storage device fopen, _wfopen Open stream fprintf, fwprintf Write formatted data to stream fputc, fputwc Write a character to a stream (function versions of putc and putwc) _fputchar, _fputwchar Write character to stdout (function versions of putchar and putwchar) fputs, fputws Write string to stream fread Read unformatted data from stream freopen, _wfreopen Reassign FILE stream pointer to new file or device fscanf, fwscanf Read formatted data from stream fseek Move file position to given location fsetpos Set position indicator of stream _fsopen, _wfsopen Open stream with file sharing ftell Get current file position fwrite Write unformatted data items to stream getc, getwc Read character from stream (macro versions of fgetc and fgetwc) getchar, getwchar Read character from stdin (macro versions of fgetchar and fgetwchar) gets, getws Read line from stdin _getw Read binary int from stream printf, wprintf Write formatted data to stdout putc, putwc Write character to a stream (macro versions of fputc and fputwc) putchar, putwchar Write character to stdout (macro versions of fputchar and fputwchar) puts, _putws Write line to stream _putw Write binary int to stream rewind Move file position to beginning of stream _rmtmp Remove temporary files created by tmpfile scanf, wscanf Read formatted data from stdin setbuf Control stream buffering _setmaxstdio Set a maximum for the number of simultaneously open files at the stream I/O level. setvbuf Control stream buffering and buffer size _snprintf, _snwprintf Write formatted data of specified length to string sprintf, swprintf Write formatted data to string sscanf, swscanf Read formatted data from string _tempnam, _wtempnam Generate temporary filename in given directory tmpfile Create temporary file tmpnam, _wtmpnam Generate temporary filename ungetc, ungetwc Push character back onto stream vfprintf, vfwprintf Write formatted data to stream vprintf, vwprintf Write formatted data to stdout _vsnprintf, _vsnwprintf Write formatted data of specified length to buffer vsprintf, vswprintf Write 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.)  
  • Opening and Closing Files: How to open and close file descriptors.
  • I/O Primitives: Reading and writing data.
  • File Position Primitive: Setting a descriptor's file position.
  • Descriptors and Streams: Converting descriptor to stream or vice-versa.
  • Stream/Descriptor Precautions: Precautions needed if you use both descriptors and streams.
  • Scatter-Gather: Fast I/O to discontinuous buffers.
  • Memory-mapped I/O: Using files like memory.
  • Waiting for I/O: How to check for input or output on multiple file descriptors.
  • Synchronizing I/O: Making sure all I/O actions completed.
  • Asynchronous I/O: Perform I/O in parallel.
  • Control Operations: Various other operations on file descriptors.
  • Duplicating Descriptors: Fcntl commands for duplicating file descriptors.
  • Descriptor Flags: Fcntl commands for manipulating flags associated with file descriptors.
  • File Status Flags: Fcntl commands for manipulating flags associated with open files.
  • File Locks: Fcntl commands for implementing file locking.
  • Interrupt Input: Getting an asynchronous signal when input arrives.
  • IOCTLs: Generic I/O Control operations.

    其他关于LowLevel I/O的文章:

    http://www.chemie.fu-berlin.de/chemnet/use/info/libc/libc_8.html
    http://www.gnu.org/software/libc/manual/html_node/Low_002dLevel-I_002fO.html
    http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=208
    http://www.thinkage.ca/english/gcos/expl/c/lib/open.html

  • 原创粉丝点击