[#BloombergLP-bdls-FilesystemUtil-open-0f] = xref:BloombergLP.adoc[BloombergLP]::xref:BloombergLP/bdls.adoc[bdls]::xref:BloombergLP/bdls/FilesystemUtil.adoc[FilesystemUtil]::open :relfileprefix: ../../../ :mrdocs: `open` overloads == Synopses Declared in `<bdls_filesystemutil.h>` Open the file at the specified `path`, using the specified `openPolicy` to determine whether to open an existing file or create a new file, and using the specified `ioPolicy` to determine whether the file will be opened for reading, writing, or both. Optionally specify a `truncatePolicy` to determine whether any contents of the file will be deleted before `open` returns. If `truncatePolicy` is not supplied, the value `e_KEEP` will be used. Return a valid `FileDescriptor` for the file on success, or `k_INVALID_FD` otherwise. If `openPolicy` is `e_OPEN`, the file will be opened if it exists, and `open` will fail otherwise. If `openPolicy` is `e_CREATE` or `e_CREATE_PRIVATE`, and no file exists at `path`, a new file will be created, and `open` will fail otherwise. If `openPolicy` is `e_CREATE_PRIVATE`, the file will be created with access restricted to the same userid as the caller in environments where that is supported (which does not necessarily include Windows) otherwise the system default access policy is used (e.g. '0777 & ~umask'). If openPolicy is e_OPEN_OR_CREATE, the file will be opened if it exists, and a new file will be created otherwise. If `ioPolicy` is `e_READ_ONLY`, the returned `FileDescriptor` will allow only read operations on the file. If `ioPolicy` is `e_WRITE_ONLY` or `e_APPEND_ONLY`, the returned `FileDescriptor` will allow only write operations on the file. If `ioPolicy` is `e_READ_WRITE` or `e_READ_APPEND`, the returned `FileDescriptor` will allow both read and write operations on the file. Additionally, if `ioPolicy` is `e_APPEND_ONLY` or `e_READ_APPEND` all writes will be made to the end of the file ("append mode"). If `truncatePolicy` is `e_TRUNCATE`, the file will have zero length when `open` returns. If `truncatePolicy` is `e_KEEP`, the file will be opened with its existing contents, if any. Note that when a file is opened in `append` mode, all writes will go to the end of the file, even if there has been seeking on the file descriptor or another process has changed the length of the file. Append‐mode writes are not atomic except in limited cases; another thread, or even another process, operating on the file may cause output not to be written, unbroken, to the end of the file. (Unix environments writing to local file systems may promise more.) Note that `open` will fail to open a file with a `truncatePolicy` of `e_TRUNCATE` unless at least one of the following policies is specified for `openPolicy` or `ioPolicy`: * `e_CREATE` * `e_CREATE_PRIVATE` * `e_OPEN_OR_CREATE` * 'e_WRITE_ONLY * `e_READ_WRITE` The parameterized `STRING_TYPE` must be one of `bsl::string`, `std::string`, `std::pmr::string` (if supported), `bsl::string_view`, or `bslstl::StringRef`. [source,cpp,subs="verbatim,replacements,macros,-callouts"] ---- static xref:BloombergLP/bdls/FilesystemUtil/FileDescriptor.adoc[FileDescriptor] xref:BloombergLP/bdls/FilesystemUtil/open-01.adoc[open]( char const* path, xref:BloombergLP/bdls/FilesystemUtil/FileOpenPolicy.adoc[FileOpenPolicy] openPolicy, xref:BloombergLP/bdls/FilesystemUtil/FileIOPolicy.adoc[FileIOPolicy] ioPolicy, xref:BloombergLP/bdls/FilesystemUtil/FileTruncatePolicy.adoc[FileTruncatePolicy] truncatePolicy = e_KEEP); ---- [.small]#xref:BloombergLP/bdls/FilesystemUtil/open-01.adoc[_» more..._]# Same as the overload taking `const char *`. [source,cpp,subs="verbatim,replacements,macros,-callouts"] ---- template<class STRING_TYPE> static xref:BloombergLP/bdls/FilesystemUtil/FileDescriptor.adoc[FilesystemUtil::FileDescriptor] xref:BloombergLP/bdls/FilesystemUtil/open-0a.adoc[open]( STRING_TYPE const& path, xref:BloombergLP/bdls/FilesystemUtil/FileOpenPolicy.adoc[FileOpenPolicy] openPolicy, xref:BloombergLP/bdls/FilesystemUtil/FileIOPolicy.adoc[FileIOPolicy] ioPolicy, xref:BloombergLP/bdls/FilesystemUtil/FileTruncatePolicy.adoc[FileTruncatePolicy] truncatePolicy = e_KEEP); ---- [.small]#xref:BloombergLP/bdls/FilesystemUtil/open-0a.adoc[_» more..._]# [.small]#Created with https://www.mrdocs.com[MrDocs]#