Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#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: Sachin Garg <schngrg@…> 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)

comment:1 by Steven Watanabe, 13 years ago

If you know at compile time whether you are only going to read/write you should use ifstream or ofstream instead.

in reply to:  1 comment:2 by Sachin Garg <schngrg@…>, 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 Sachin Garg <schngrg@…>, 13 years ago

Cc: schngrg@… 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*)

comment:4 by Beman Dawes, 13 years ago

Resolution: fixed
Status: newclosed

(In [59622]) Fix #3884

comment:5 by Beman Dawes, 13 years ago

(In [59623]) Fix #3884

Note: See TracTickets for help on using tickets.