Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
_fsopen, _wfsopen

_fsopen, _wfsopen

Visual Studio 6.0

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 );

Function Required Header Optional Headers Compatibility
_fsopen <stdio.h> <share.h>1 Win 95, Win NT
_wfsopen <stdio.h> or <wchar.h> <share.h>1 Win NT

1 For manifest constant for shflag parameter.

For additional compatibility information, see Compatibility in the Introduction.


LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version

Return Value

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



Name of file to open


Type of access permitted


Type of sharing allowed


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



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

void 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.

Stream I/O Routines

See Also   fclose, _fdopen, ferror, _fileno, fopen, freopen, _open, _setmode, _sopen

© 2015 Microsoft