File Handling

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at File Handling.

Use these routines to create, delete, and manipulate files and to set and check file-access permissions.

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. Use _setmaxstdio to change this number.

The following routines operate on files designated by a file descriptor.

File-Handling Routines (File Descriptor)

Routine Use .NET Framework equivalent
_chsize,_chsize_s Change file size System::IO::Stream::SetLength, System::IO::FileStream::SetLength
_filelength, _filelengthi64 Get file length System::IO::Stream::Length, System::IO::FileStream::Length
_fstat, _fstat32, _fstat64, _fstati64, _fstat32i64, _fstat64i32 Get file-status information on descriptor Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
_get_osfhandle Return operating-system file handle associated with existing C run-time file descriptor Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
_isatty Check for character device System::IO::Stream::CanWrite, System::IO::FileStream::CanWrite
_locking Lock areas of file System::IO::FileStream::Lock
_open_osfhandle Associate C run-time file descriptor with existing operating-system file handle System::IO::FileStream::Handle
_setmode Set file-translation mode System::IO::BinaryReader Class, System::IO::TextReader Class

The following routines operate on files specified by a path or filename.

File-Handling Routines (Path or Filename)

Routine Use .NET Framework equivalent
_access, _waccess, _access_s, _waccess_s Check file-permission setting System::IO::FileAccess Enumeration
_chmod, _wchmod Change file-permission setting System::IO::File::SetAttributes, System::Security::Permissions::FileIOPermission
_fullpath, _wfullpath Expand a relative path to its absolute path name System::IO::File::Create
_makepath, _wmakepath, _makepath_s, _wmakepath_s Merge path components into single, full path System::IO::File::Create
_mktemp, _wmktemp, _mktemp_s, _wmktemp_s Create unique filename Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
remove, _wremove Delete file System::IO::File::Delete
rename, _wrename Rename file System::IO::File::Move
_splitpath, _wsplitpath, _splitpath_s, _wsplitpath_s Parse path into components Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
_stat, _stat64, _stati64, _wstat, _wstat64, _wstati64 Get file-status information on named file System::IO::File::GetAttributes, System::IO::File::GetCreationTime, System::IO::File::GetLastAccessTime, System::IO::File::GetLastWriteTime
_umask, _umask_s Set default permission mask for new files created by program System::IO::File::SetAttributes
_unlink, _wunlink Delete file System::IO::File::Delete

The following routines open files.

File-Handling Routines (Open File)

Routine Use .NET Framework equivalent
fopen, _wfopen, fopen_s, _wfopen_s Opens a file and returns a pointer to the open file. System::IO::File::Open, FileStream
_fsopen, _wfsopen Open a stream with file sharing and returns a pointer to the open file. System::IO::File::Open, FileStream
_open, _wopen Opens a file and returns a file descriptor to the opened file. System::IO::File::Open, FileStream
_sopen, _wsopen, _sopen_s, _wsopen_s Open a file with file sharing and returns a file descriptor to the open file.
_pipe Creates a pipe for reading and writing. Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
freopen, _wfreopen, freopen_s, _wfreopen_s Reassign a file pointer. System::IO::File::Open, FileStream

The following functions provide a way to change the representation of the file between a FILE structure, a file descriptor, and a Win32 file handle.

_fdopen, _wfdopen Associates a stream with a file that was previously opened for low-level I/O and returns a pointer to the open stream. System::IO::File::Open
_fileno Gets the file descriptor associated with a stream. System::IO::FileStream::Handle
_get_osfhandle Return operating-system file handle associated with existing C run-time file descriptor Not applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
_open_osfhandle Associates C run-time file descriptor with an existing operating-system file handle. System::IO::FileStream::Handle

The following Win32 functions also open files and pipes:

See Also

Run-Time Routines by Category
Directory Control
System Calls