_fsopen, _wfsopen
Go Up to stdio.h Index
Header File
stdio.h, share.h
Category
Input/output Routines
Prototype
FILE *_fsopen(const char *filename, const char *mode, int shflag);
FILE *_wfsopen(const wchar_t *filename, const wchar_t *mode, int shflag);
Description
Opens a stream with file sharing.
_fsopen opens the file named by filename and associates a stream with it. _fsopen returns a pointer that is used to identify the stream in subsequent operations.
The mode string used in calls to _fsopen is one of the following values:
Item | Description |
---|---|
r |
Open for reading only. |
w |
Create for writing. If a file by that name already exists, it will be overwritten. |
a |
Append; open for writing at end of file. or create for writing if the file does not exist. |
r+ |
Open an existing file for update (reading and writing). |
w+ |
Create a new file for update (reading and writing). If a file by that name already exists, it will be overwritten. |
a+ |
Open for append; open (or create if the file does not exist) for update at the end of the file. |
To specify that a given file is being opened or created in text mode, append a t to the mode string (rt w+t and so on). Similarly, to specify binary mode, append a b to the mode string (wb a+b and so on). _fsopen also allows the t or b to be inserted between the letter and the + character in the mode string; for example rt+ is equivalent to r+t. If a t or b is not given in the mode string, the mode is governed by the global variable _fmode:
- If _fmode is set to O_BINARY, files are opened in binary mode.
- If _fmode is set to O_TEXT, files are opened in text mode.
These O_... constants are defined in fcntl.h.
When a file is opened for update, both input and output can be done on the resulting stream, except that:
- Output cannot be directly followed by input without an intervening fseek or rewind.
- Input cannot be directly followed by output without an intervening fseek, rewind, or an input that encounters end-of-file
shflag specifies the type of file-sharing allowed on the file filename. Symbolic constants for shflag are defined in share.h.
SH_COMPAT |
Sets compatibility mode |
SH_DENYRW |
Denies read/write access |
SH_DENYWR |
Denies write access |
SH_DENYRD |
Denies read access |
SH_DENYNONE |
Permits read/write access |
SH_DENYNO |
Permits read/write access |
Return Value
On successful completion _fsopen returns a pointer to the newly opened stream.
On error it returns NULL.
Example
#include <io.h> #include <process.h> #include <share.h> #include <stdio.h> int main(void) { FILE *f; int status; f = _fsopen("TESTFILE.DAT", "r", SH_DENYNO); if (f == NULL) { printf("_fsopen failed\n"); exit(1); } status = access("TESTFILE.DAT", 6); if (status == 0) printf("read/write access allowed\n"); else printf("read/write access not allowed\n"); fclose(f); return 0; }
Portability
POSIX | Win32 | ANSI C | ANSI C++ | |
---|---|---|---|---|
_fsopen |
+ |
|||
_wfsopen |
NT only |