_fsopen, _wfsopen
Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

_fsopen, _wfsopen

Open a stream with file sharing.

FILE *_fsopen( 
   const char *filename,
   const char *mode,
   int shflag 
FILE *_wfsopen( 
   const wchar_t *filename,
   const wchar_t *mode,
   int shflag 


Name of file to open.
Type of access permitted.
Type of sharing allowed.

Return Value

Each of these functions returns a pointer to the stream. A NULL pointer value indicates an error.


The _fsopen function opens the file specified by filename as a stream and prepares the file for subsequent shared reading or writing, as defined by the mode and shflag arguments. _wfsopen is a wide-character version of _fsopen; the filename and mode arguments to _wfsopen are wide-character strings. _wfsopen and _fsopen behave identically otherwise.

Generic-Text Routine Mappings

TCHAR.H routine _UNICODE & _MBCS not defined _MBCS defined _UNICODE defined
_tfsopen _fsopen _fsopen _wfsopen

The character string mode specifies the type of access requested for the file, as follows:

Opens for reading. If the file does not exist or cannot be found, the _fsopen call fails.
Opens an empty file for writing. If the given file exists, its contents are destroyed.
Opens for writing at the end of the file (appending); creates the file first if it does not exist.
Opens for both reading and writing. (The file must exist.)
Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.
Opens for reading and appending; creates the file first if it does not exist.

Use the "w" and "w+" types with care, as they can destroy existing files.

When a file is opened with the "a" or "a+" access type, all write operations occur at the end of the file. The file pointer can be repositioned using fseek or rewind, but is always moved back to the end of the file before any write operation is carried out. Thus existing data cannot be overwritten. When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for "update"). However, when switching between reading and writing, there must be an intervening fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired. In addition to the above values, one of the following characters can be included in mode to specify the translation mode for new lines:

Opens a file in text (translated) mode. In this mode, carriage return–linefeed (CR-LF) combinations are translated into single linefeeds (LF) on input and LF characters are translated to CR-LF combinations on output. Also, CTRL+Z is interpreted as an end-of-file character on input. In files opened for reading or reading/writing, _fsopen checks for a CTRL+Z at the end of the file and removes it, if possible. This is done because using fseek and ftell to move within a file that ends with a CTRL+Z may cause fseek to behave improperly near the end of the file.
Opens a file in binary (untranslated) mode; the above translations are suppressed.

If t or b is not given in mode, the translation mode is defined by the default-mode variable _fmode. If t or b is prefixed to the argument, the function fails and returns NULL. For a discussion of text and binary modes, see Text and Binary Mode File I/O.

The argument shflag is a constant expression consisting of one of the following manifest constants, defined in SHARE.H:

Sets Compatibility mode for 16-bit applications.
Permits read and write access.
Denies read access to file.
Denies read and write access to file.
Denies write access to file.


Function Required header Optional headers Compatibility
_fsopen <stdio.h> <share.h>1 Win 98, Win Me, Win NT, Win 2000, Win XP
_wfsopen <stdio.h> or <wchar.h> <share.h>1 Win NT, Win 2000, Win XP

1 For manifest constant for shflag parameter.

For additional compatibility information, see Compatibility in the Introduction.


All versions of the C run-time libraries.


// crt_fsopen.c

#include <stdio.h>
#include <stdlib.h>
#include <share.h>

int main( void )
   FILE *stream;

   /* Open output file for writing. Using _fsopen allows us to
    * ensure that no one else writes to the file while we are
    * writing to it.
   if( (stream = _fsopen( "outfile", "wt", _SH_DENYWR )) != NULL )
      fprintf( stream, "No one else in the network can write "
                       "to this file until we are done.\n" );
      fclose( stream );
   /* Now others can write to the file while we read it. */
   system( "type outfile" );


No one else in the network can write to this file until we are done.

See Also

Stream I/O Routines | fclose | _fdopen | ferror | _fileno | fopen | freopen | _open | _setmode | _sopen | Run-Time Routines and .NET Framework Equivalents

© 2016 Microsoft