open a file and associate a stream with it
#include <stdio.h> #include <share.h> FILE *_fsopen( const char *filename, const char *mode, int share );
The _fsopen() function opens the file whose name is the string pointed to by filename, and associates a stream with it. The arguments mode and share control shared reading or writing. The argument mode points to a string beginning with one of the following sequences:
The letter b may be added to any of the above sequences in the second or third position to indicate that the file is (or must be) a binary file (an ANSI requirement for portability to systems that make a distinction between text and binary files). Under QNX, there is no difference between text files and binary files.
The shared access for the file, share, is established by a combination of bits defined in the share.h header file. The following values may be set:
Note that
fopen( filename, mode );
is the same as:
_fsopen( filename, mode, SH_COMPAT );
The _fsopen() function returns a pointer to the object controlling the stream. This pointer must be passed as a parameter to subsequent functions for performing operations on the file. If the open operation fails, _fsopen() returns NULL. When an error has occurred, errno contains a value that indicates the type of error that has been detected.
errno, fclose(), fcloseall(), fdopen(), fopen(), freopen()
#include <stdio.h> #include <share.h> void main() { FILE *fp; /* Open a file, and prevent others from writing to it. */ fp = _fsopen( "report.dat", "w", SH_DENYWR ); if( fp != NULL ) { /* rest of code goes here */ fclose( fp ); } }
WATCOM
All (except DOS/PM)