Export (0) Print
Expand All
div
Expand Minimize
0 out of 2 rated this helpful - Rate this topic

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

Parameters

filename
Name of file to open.
mode
Type of access permitted.
shflag
Type of sharing allowed.

Return Value

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

Remarks

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:

"r"
Opens for reading. If the file does not exist or cannot be found, the _fsopen call fails.
"w"
Opens an empty file for writing. If the given file exists, its contents are destroyed.
"a"
Opens for writing at the end of the file (appending); creates the file first if it does not exist.
"r+"
Opens for both reading and writing. (The file must exist.)
"w+"
Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.
"a+"
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:

t
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.
b
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:

_SH_COMPAT
Sets Compatibility mode for 16-bit applications.
_SH_DENYNO
Permits read and write access.
_SH_DENYRD
Denies read access to file.
_SH_DENYRW
Denies read and write access to file.
_SH_DENYWR
Denies write access to file.

Requirements

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.

Libraries

All versions of the C run-time libraries.

Example

// 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" );
}

Output

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.