流 I/OStream 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. 流缓冲区的默认大小为 4K。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.
流 I/O 例程Stream I/O Routines
例程所返回的值Routine | 使用Use |
---|---|
clearerr、 clearerr_sclearerr, clearerr_s | 清除流的错误指示器Clear error indicator for stream |
fclosefclose | 关闭流Close stream |
_fcloseall_fcloseall | 关闭除 stdin、stdout 和 stderr 之外的所有流Close all open streams except stdin, stdout, and stderr |
_fdopen、wfdopen_fdopen, wfdopen | 将流与打开的文件的文件描述符相关联Associate stream with file descriptor of open file |
feoffeof | 在流上测试文件尾Test for end of file on stream |
ferrorferror | 在流上测试错误Test for error on stream |
fflushfflush | 刷新到缓冲区或存储设备的流Flush stream to buffer or storage device |
fgetc、fgetwcfgetc, fgetwc | 从流(getc 和 getwc 的函数版本)读取字符Read character from stream (function versions of getc and getwc) |
_fgetchar、_fgetwchar_fgetchar, _fgetwchar | 从 stdin(getchar 和 getwchar 的函数版本)读取字符Read character from stdin (function versions of getchar and getwchar) |
fgetposfgetpos | 获取流的位置指示器Get position indicator of stream |
fgets、fgetwsfgets, fgetws | 从流读取字符串Read string from stream |
_fileno_fileno | 获取与流关联的文件描述符Get file descriptor associated with stream |
_flushall_flushall | 刷新所有到缓冲区或存储设备的流Flush all streams to buffer or storage device |
fopen、_wfopen、 fopen_s, _wfopen_sfopen, _wfopen, fopen_s, _wfopen_s | 打开流Open stream |
fprintf、_fprintf_l、fwprintf、_fwprintf_l、fprintf_s、_fprintf_s_l、fwprintf_s、_fwprintf_s_lfprintf, _fprintf_l, fwprintf, _fwprintf_l, fprintf_s, _fprintf_s_l, fwprintf_s, _fwprintf_s_l | 将格式化数据写入流Write formatted data to stream |
fputc、fputwcfputc, fputwc | 将字符写入流(putc 和 putwc 的函数版本)Write a character to a stream (function versions of putc and putwc) |
_fputchar、_fputwchar_fputchar, _fputwchar | 将字符写入 stdout(putchar 和 putwchar 的函数版本)Write character to stdout (function versions of putchar and putwchar) |
fputs、fputwsfputs, fputws | 将字符串写入流Write string to stream |
freadfread | 从流中读取未格式化的数据Read unformatted data from stream |
freopen、_wfreopen、 freopen_s, _wfreopen_sfreopen, _wfreopen, freopen_s, _wfreopen_s | 重新将 FILE 流指针分配到新的文件或设备Reassign FILE stream pointer to new file or device |
fscanf、fwscanf、fscanf_s、_fscanf_s_l、fwscanf_s、_fwscanf_s_lfscanf, fwscanf, fscanf_s, _fscanf_s_l, fwscanf_s, _fwscanf_s_l | 从流中读取格式化数据Read formatted data from stream |
fseek、_fseeki64fseek, _fseeki64 | 将文件位置移动到给定位置Move file position to given location |
fsetposfsetpos | 设置流的位置指示器Set position indicator of stream |
_fsopen、_wfsopen_fsopen, _wfsopen | 打开具有文件共享的流Open stream with file sharing |
ftell、_ftelli64ftell, _ftelli64 | 获取当前文件位置Get current file position |
fwritefwrite | 将未格式化的数据项目写入流Write unformatted data items to stream |
getc、getwcgetc, getwc | 从流(fgetc 和 fgetwc 的宏版本)读取字符Read character from stream (macro versions of fgetc and fgetwc) |
getchar、getwchargetchar, getwchar | 从 stdin(fgetchar 和 fgetwchar 的宏版本)读取字符Read character from stdin (macro versions of fgetchar and fgetwchar) |
_getmaxstdio_getmaxstdio | 返回在 I/O 流级别允许同时打开的文件数。Returns the number of simultaneously open files permitted at the stream I/O level. |
gets_s、_getws_sgets_s, _getws_s | 从 stdin 读取行Read line from stdin |
_getw_getw | 从流读取二进制 intRead binary int from stream |
printf、_printf_l、wprintf、_wprintf_l、printf_s、_printf_s_l、wprintf_s、_wprintf_s_lprintf, _printf_l, wprintf, _wprintf_l,printf_s, _printf_s_l, wprintf_s, _wprintf_s_l | 将格式化数据写入 stdoutWrite formatted data to stdout |
putc、putwcputc, putwc | 将字符写入流(fputc 和 fputwc 的宏版本)Write character to a stream (macro versions of fputc and fputwc) |
putchar、putwcharputchar, putwchar | 将字符写入 stdout(fputchar 和 fputwchar 的宏版本)Write character to stdout (macro versions of fputchar and fputwchar) |
puts、_putwsputs, _putws | 将行写入流Write line to stream |
_putw_putw | 将二进制 int 写入流Write binary int to stream |
rewindrewind | 将文件位置移动到的流的开头Move file position to beginning of stream |
_rmtmp_rmtmp | 删除由 tmpfile 创建的临时文件Remove temporary files created by tmpfile |
scanf、_scanf_l、wscanf、_wscanf_l、scanf_s、_scanf_s_l、wscanf_s、_wscanf_s_lscanf, _scanf_l, wscanf, _wscanf_l,scanf_s, _scanf_s_l, wscanf_s, _wscanf_s_l | 从 stdin 读取格式化数据Read formatted data from stdin |
setbufsetbuf | 控制流缓冲Control stream buffering |
_setmaxstdio_setmaxstdio | 设置在流 I/O 级别同时打开的最大文件数。Set a maximum for the number of simultaneously open files at the stream I/O level. |
setvbufsetvbuf | 控制流缓冲和缓冲区大小Control stream buffering and buffer size |
_snprintf、_snwprintf、 _snprintf_s, _snprintf_s_l, _snwprintf_s, _snwprintf_s_l_snprintf, _snwprintf, _snprintf_s, _snprintf_s_l, _snwprintf_s, _snwprintf_s_l | 将指定长度的格式化数据写入字符串Write formatted data of specified length to string |
_snscanf、_snwscanf、_snscanf_s、_snscanf_s_l、_snwscanf_s、_snwscanf_s_l_snscanf, _snwscanf, _snscanf_s, _snscanf_s_l, _snwscanf_s, _snwscanf_s_l | 从标准输入流读取指定长度的格式化数据。Read formatted data of a specified length from the standard input stream. |
sprintf、swprintf、sprintf_s、_sprintf_s_l、swprintf_s、_swprintf_s_lsprintf, swprintf, sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l | 将格式化数据写入字符串Write formatted data to string |
sscanf、swscanf、sscanf_s、_sscanf_s_l、swscanf_s、_swscanf_s_lsscanf, swscanf, sscanf_s, _sscanf_s_l, swscanf_s, _swscanf_s_l | 从字符串读取格式化数据Read formatted data from string |
_tempnam,_wtempnam_tempnam, _wtempnam | 在给定目录中生成临时文件名Generate temporary filename in given directory |
tmpfile、 tmpfile_stmpfile, tmpfile_s | 创建临时文件Create temporary file |
tmpnam、_wtmpnam、 tmpnam_s, _wtmpnam_stmpnam, _wtmpnam, tmpnam_s, _wtmpnam_s | 生成临时文件名Generate temporary filename |
ungetc、ungetwcungetc, ungetwc | 将字符推送回流上Push character back onto stream |
_vcprintf、_vcwprintf、_vcprintf_s、_vcprintf_s_l、_vcwprintf_s、_vcwprintf_s_l_vcprintf, _vcwprintf, _vcprintf_s, _vcprintf_s_l, _vcwprintf_s, _vcwprintf_s_l | 将格式化数据写入控制台。Write formatted data to the console. |
vfprintf、vfwprintf、vfprintf_s、_vfprintf_s_l、vfwprintf_s、_vfwprintf_s_lvfprintf, vfwprintf, vfprintf_s, _vfprintf_s_l, vfwprintf_s, _vfwprintf_s_l | 将格式化数据写入流Write formatted data to stream |
vprintf、vwprintf、vprintf_s、_vprintf_s_l、vwprintf_s、_vwprintf_s_lvprintf, vwprintf, vprintf_s, _vprintf_s_l, vwprintf_s, _vwprintf_s_l | 将格式化数据写入 stdoutWrite formatted data to stdout |
_vsnprintf、_vsnwprintf、vsnprintf_s、_vsnprintf_s、_vsnprintf_s_l、_vsnwprintf_s、_vsnwprintf_s_l_vsnprintf, _vsnwprintf, vsnprintf_s, _vsnprintf_s, _vsnprintf_s_l, _vsnwprintf_s, _vsnwprintf_s_l | 将指定长度的格式化数据写入缓冲区Write formatted data of specified length to buffer |
vsprintf、vswprintf、vsprintf_s、_vsprintf_s_l、vswprintf_s、_vswprintf_s_lvsprintf, vswprintf, vsprintf_s, _vsprintf_s_l, vswprintf_s, _vswprintf_s_l | 将格式化数据写入缓冲区Write formatted data to buffer |
当程序开始执行时,启动代码将自动开启若干流:标准输入(由 stdin 指向)、标准输出(由 stdout 指向)和标准错误(由 stderr 指向)。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. 使用 freopen,将 stdin、stdout 或 stderr 重定向到磁盘文件或设备。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. 当 stdout 和 stderr 函数是完整的,或在进行了每次库调用之后要写入字符设备,则这将刷新两个函数。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. 使用 fflush 或 _flushall 以确保与指定文件相关联的缓冲区或所有打开的缓冲区被刷新到操作系统,操作系统在将数据写入磁盘之前可缓存数据。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:
与文件 COMMODE.OBJ 链接以设置全局提交标志。Link with the file COMMODE.OBJ to set a global commit flag. 全局标志的默认设置是 n,意味着“不提交”。The default setting of the global flag is n, for "no-commit."
使用 fopen 或 _fdopen 将模式标志设置为 c。Set the mode flag to c with fopen or _fdopen.
专门使用 c 或 n 标志打开的任何文件的行为以该标志为准,而不考虑全局提交/不提交标志的状态。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. 请参阅 _setmaxstdio 获取有关此限制的信息。See _setmaxstdio for information on this limit.
只有通过对 fflush 或对文件定位函数(fseek、fsetpos 或 rewind)进行干预调用时,输入才能直接跟随输出。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.
请参阅See Also
输入和输出Input and Output
按类别分的通用 C 运行时例程Universal C runtime routines by category
反馈
我们衷心期待你的反馈意见。 请选择要提供的反馈类型:
我们的反馈系统是在 GitHub Issues 的基础之上构建而成。 请访问我们的博客,了解详细信息。
正在加载反馈...