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)

RoutineUse.NET Framework equivalent
_chsize,_chsize_sChange file sizeSystem::IO::Stream::SetLength, System::IO::FileStream::SetLength
_filelength, _filelengthi64Get file lengthSystem::IO::Stream::Length, System::IO::FileStream::Length
_fstat, _fstat32, _fstat64, _fstati64, _fstat32i64, _fstat64i32Get file-status information on descriptorNot applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
_get_osfhandleReturn operating-system file handle associated with existing C run-time file descriptorNot applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
_isattyCheck for character deviceSystem::IO::Stream::CanWrite, System::IO::FileStream::CanWrite
_lockingLock areas of fileSystem::IO::FileStream::Lock
_open_osfhandleAssociate C run-time file descriptor with existing operating-system file handleSystem::IO::FileStream::Handle
_setmodeSet file-translation modeSystem::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)

RoutineUse.NET Framework equivalent
_access, _waccess, _access_s, _waccess_sCheck file-permission settingSystem::IO::FileAccess Enumeration
_chmod, _wchmodChange file-permission settingSystem::IO::File::SetAttributes, System::Security::Permissions::FileIOPermission
_fullpath, _wfullpathExpand a relative path to its absolute path nameSystem::IO::File::Create
_makepath, _wmakepath, _makepath_s, _wmakepath_sMerge path components into single, full pathSystem::IO::File::Create
_mktemp, _wmktemp, _mktemp_s, _wmktemp_sCreate unique filenameNot applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
remove, _wremoveDelete fileSystem::IO::File::Delete
rename, _wrenameRename fileSystem::IO::File::Move
_splitpath, _wsplitpath, _splitpath_s, _wsplitpath_sParse path into componentsNot applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
_stat, _stat64, _stati64, _wstat, _wstat64, _wstati64Get file-status information on named fileSystem::IO::File::GetAttributes, System::IO::File::GetCreationTime, System::IO::File::GetLastAccessTime, System::IO::File::GetLastWriteTime
_umask, _umask_sSet default permission mask for new files created by programSystem::IO::File::SetAttributes
_unlink, _wunlinkDelete fileSystem::IO::File::Delete

The following routines open files.

File-Handling Routines (Open File)

RoutineUse.NET Framework equivalent
fopen, _wfopen, fopen_s, _wfopen_sOpens a file and returns a pointer to the open file.System::IO::File::Open, FileStream
_fsopen, _wfsopenOpen a stream with file sharing and returns a pointer to the open file.System::IO::File::Open, FileStream
_open, _wopenOpens a file and returns a file descriptor to the opened file.System::IO::File::Open, FileStream
_sopen, _wsopen, _sopen_s, _wsopen_sOpen a file with file sharing and returns a file descriptor to the open file.
_pipeCreates 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_sReassign 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, _wfdopenAssociates 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
_filenoGets the file descriptor associated with a stream.System::IO::FileStream::Handle
_get_osfhandleReturn operating-system file handle associated with existing C run-time file descriptorNot applicable. To call the standard C function, use PInvoke. For more information, see Platform Invoke Examples.
_open_osfhandleAssociates 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:

Run-Time Routines by Category
Directory Control
System Calls

Show: