文件处理File Handling

使用这些例程可创建、删除和操作文件以及设置和检查文件访问权限。Use these routines to create, delete, and manipulate files and to set and check file-access permissions.

C 运行时库将任何时候可以打开的文件数限制为 512。The C run-time libraries have a 512 limit for the number of files that can be open at any one time. 试图打开超过最大数量的文件描述符或文件流会导致程序失败。Attempting to open more than the maximum number of file descriptors or file streams causes program failure. 使用 _setmaxstdio 可更改此数量。Use _setmaxstdio to change this number.

文件处理例程(文件描述符)File-Handling Routines (File Descriptor)

这些例程对文件描述符指定的文件进行操作。These routines operate on files designated by a file descriptor.

例程Routine 使用Use
_chsize_chsize_s_chsize,_chsize_s 更改文件大小Change file size
_filelength、_filelengthi64_filelength, _filelengthi64 获取文件长度Get file length
_fstat、_fstat32、_fstat64、_fstati64、_fstat32i64、_fstat64i32_fstat, _fstat32, _fstat64, _fstati64, _fstat32i64, _fstat64i32 获取有关描述符的文件状态信息Get file-status information on descriptor
_get_osfhandle_get_osfhandle 返回与现有 C 运行时文件描述符关联的操作系统文件句柄Return operating-system file handle associated with existing C run-time file descriptor
_isatty_isatty 检查字符设备Check for character device
_locking_locking 锁定文件的区域Lock areas of file
_open_osfhandle_open_osfhandle 将 C 运行时文件描述符与现有操作系统文件句柄关联Associate C run-time file descriptor with existing operating-system file handle
_setmode_setmode 设置文件转换模式Set file-translation mode

文件处理例程(路径或文件名)File-Handling Routines (Path or Filename)

这些例程对路径或文件名指定的文件进行操作。These routines operate on files specified by a path or filename.

例程Routine 使用Use
_access, _waccess, _access_s, _waccess_s_access, _waccess, _access_s, _waccess_s 检查文件权限设置Check file-permission setting
_chmod、_wchmod_chmod, _wchmod 更改文件权限设置Change file-permission setting
_fullpath、_wfullpath_fullpath, _wfullpath 将相对路径扩展为其绝对路径名Expand a relative path to its absolute path name
_makepath, _wmakepath, _makepath_s, _wmakepath_s_makepath, _wmakepath, _makepath_s, _wmakepath_s 将路径组件合并为单个完整路径Merge path components into single, full path
_mktemp、_wmktemp_mktemp_s, _wmktemp_s_mktemp, _wmktemp, _mktemp_s, _wmktemp_s 创建唯一的文件名Create unique filename
remove、_wremoveremove, _wremove 删除文件Delete file
rename、_wrenamerename, _wrename 重命名文件Rename file
_splitpath、_wsplitpath_splitpath_s, _wsplitpath_s_splitpath, _wsplitpath, _splitpath_s, _wsplitpath_s 将路径分析为组件Parse path into components
_stat、_stat64、_stati64、_wstat、_wstat64、_wstati64_stat, _stat64, _stati64, _wstat, _wstat64, _wstati64 获取有关命名文件的文件状态信息Get file-status information on named file
_umask, _umask_s_umask, _umask_s 为程序创建的新文件设置默认权限掩码Set default permission mask for new files created by program
_unlink、_wunlink_unlink, _wunlink 删除文件Delete file

文件处理例程(打开文件)File-Handling Routines (Open File)

这些例程打开文件。These routines open files.

例程Routine 使用Use
fopen, _wfopen, fopen_s, _wfopen_sfopen, _wfopen, fopen_s, _wfopen_s 打开文件并返回指向打开的文件的指针。Opens a file and returns a pointer to the open file.
_fsopen、_wfsopen_fsopen, _wfsopen 打开具有文件共享的流并返回指向打开的文件的指针。Open a stream with file sharing and returns a pointer to the open file.
_open、_wopen_open, _wopen 打开文件并返回打开的文件的文件描述符。Opens a file and returns a file descriptor to the opened file.
_sopen, _wsopen, _sopen_s, _wsopen_s_sopen, _wsopen, _sopen_s, _wsopen_s 打开具有文件共享的文件并返回打开的文件的文件描述符。Open a file with file sharing and returns a file descriptor to the open file.
_pipe_pipe 创建用于读取和写入的管道。Creates a pipe for reading and writing.
freopen, _wfreopen, freopen_s, _wfreopen_sfreopen, _wfreopen, freopen_s, _wfreopen_s 重新分配文件指针。Reassign a file pointer.

这些例程提供一种方法,用于在 FILE 结构、文件描述符与 Win32 文件句柄之间更改文件的表示形式。These routines provide a way to change the representation of the file between a FILE structure, a file descriptor, and a Win32 file handle.

例程Routine 使用Use
_fdopen、_wfdopen_fdopen, _wfdopen 将流与以前为低级别 I/O 而打开的文件相关联,并返回指向打开的流的指针。Associates a stream with a file that was previously opened for low-level I/O and returns a pointer to the open stream.
_fileno_fileno 获取与流关联的文件描述符。Gets the file descriptor associated with a stream.
_get_osfhandle_get_osfhandle 返回与现有 C 运行时文件描述符关联的操作系统文件句柄Return operating-system file handle associated with existing C run-time file descriptor
_open_osfhandle_open_osfhandle 将 C 运行时文件描述符与现有操作系统文件句柄关联。Associates C run-time file descriptor with an existing operating-system file handle.

以下 Win32 函数也打开文件和管道:The following Win32 functions also open files and pipes:

另请参阅See Also

按类别分的运行时例程 Run-Time Routines by Category
目录控制 Directory Control
系统调用System Calls