_fsopen

Description

Opens a stream with file sharing.

#include <stdio.h>    
#include <share.h> shflag constants  

FILE *_fsopen( const char *filename, const char *mode, int shflag );

filename Filename to open  
mode Type of access permitted  
shflag Type of sharing allowed  

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.

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

Type Description

"r" Opens for reading. If the file does not exist or cannot be found, the _fsopen call will fail.
"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. Although the file pointer can be repositioned using fseek or rewind, the file pointer 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 values listed above, one of the following characters can be included in mode to specify the translation mode for new lines:

Mode Meaning

t Open in text (translated) mode. In this mode, carriage-return–line-feed (CR-LF) combinations are translated into single line feeds (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 the fseek and ftell functions to move within a file that ends with a CTRL+Z may cause fseek to behave improperly near the end of the file.
b Open 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 will fail and will return NULL.

See “Input and Output” for a discussion of text and binary modes.

The argument shflag is a constant expression consisting of one of the following manifest constants, defined in SHARE.H. If SHARE.COM—or SHARE.EXE for some versions of DOS—is not installed, DOS ignores the sharing mode. (See your system documentation for detailed information about sharing modes.)

Constant Meaning

_SH_COMPAT Sets compatibility mode
_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

The _fsopen function should be used only under DOS versions 3.0 and later. Under earlier versions of DOS, the shflag argument is ignored.

Return Value

The _fsopen function returns a pointer to the stream. A NULL pointer value indicates an error.

Compatibility

Standards:None

16-Bit:DOS, QWIN, WIN, WIN DLL

32-Bit:DOS32X

See Also

fclose, _fcloseall, _fdopen, ferror, _fileno, fopen, freopen, _open, _setmode, _sopen

Example

/* FSOPEN.C: This program opens files named “data” and “data2". It uses

* fclose to close ”data" and _fcloseall to close all remaining files.

*/

#include <stdio.h>

#include <share.h>

FILE *stream;

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

}

Output

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