#3884 closed Bugs (fixed)
boost fstream 'enforces' in AND out mode. It should be be possible to open a read-only file using fstream.
Reported by: | Owned by: | Beman Dawes | |
---|---|---|---|
Milestone: | To Be Determined | Component: | filesystem |
Version: | Boost 1.41.0 | Severity: | Problem |
Keywords: | Cc: | schngrg@… |
Description
boost fstream 'enforces' in AND out mode by adding both in and out flags to the user specified file open mode.
Check boost\filesystem\fstream.hpp line 479, it does:
mode | std::ios_base::in | std::ios_base::out
This makes it impossible to use fstream object to open a file for only reading or writing. This is especially troublesome when trying to open a read-only file as open call will fail with access denied error.
Fix: fstream should not modify 'mode' value and use the user specified value as is.
Change History (5)
follow-up: 2 comment:1 by , 13 years ago
comment:2 by , 13 years ago
Replying to steven_watanabe:
If you know at compile time whether you are only going to read/write you should use ifstream or ofstream instead.
File opening mode is decided at run-time and it workes fine when working with std::fstream objects (at least with Microsoft's implementation of it, didn't try on gcc).
We also need a common base class to hold the open file stream objects (ifstream does not inherit from fstream, it's the other way round). If we use ifstream object when opening files only for reading, we won't be able to pass the object around with other fstream objects.
comment:3 by , 13 years ago
Cc: | added |
---|
Also, boost::fstream is implemented as a wrapper over std::fstream (it uses a std::fstream object internally), which means that if any changes to 'mode' value are indeed needed, then the std::fstream::open() can do that internally. I am unable to see the reason for boost's wrapper to change its value before passing it to std::fstream.
(We are using boost's fstream because it lets us directly use wpath filenames which is is huge convenience compared to using std::fstream's char*)
If you know at compile time whether you are only going to read/write you should use ifstream or ofstream instead.