8

How can I override a C++ standard-library class function? In my application, I use ofstream objects in many different places of code. And now I want to open files in a different permission mode in Linux, Ubuntu. But open function of ofstream has no parameter to specify the permission mode of the file it creats.

Now I want to override open() function of ofstream class so it will get a parameter to specify the permissions for user access.

Peter G.
  • 13,888
  • 6
  • 51
  • 75
korhan
  • 81
  • 1
  • 2

4 Answers4

5

For starters, to clarify your terminology, the STL usually refers to the subset of the C++ standard library containing the containers, iterators, and algorithms. The streams classes are part of the C++ standard library, but are usually not bundled together with the STL. Some purists will insist that there is no such thing as the STL in the C++ standard library (since the STL is, technically speaking, a third-party library that was adopted into the standard), but most C++ programmers will know what you mean.

As for your question, there is no way within the standard to specify permission modes with ofstream. If you want to create your own custom stream class that is like ofstream but which supports permissions, your best bet would be to do the following:

  1. Create a subclass of basic_streambuf that allows you to open, write, and possibly read files while specifying Unix permissions. The streams classes are designed so that the details of communicating with physical devices like disk, networks, and the console are all handled by the basic_streambuf class and its derived classes. If you want to make your own stream class, implementing a stream buffer would be an excellent first step.

  2. Define your own class that subclasses basic_ostream and installs your custom basic_streambuf. By default, the basic_ostream supports all of the standard output routines by implementing them in terms of the underlying basic_streambuf object. Once you have your own stream buffer, building a basic_ostream class that uses that streambuf will cause all of the standard stream operations on that class (such as <<) to start making the appropriate calls to your streambuf.

If you'd like more details on this, an excellent reference is Standard C++ IOStreams and Locales. As a shameless plug, I have used the techniques from this book to build a stream class that wraps a socket connection. While a lot of the code in my stream won't be particularly useful, the basic structure should help you get started.

Hope this helps!

templatetypedef
  • 328,018
  • 92
  • 813
  • 992
1

This is not answering your question directly as I wouldn't advise overriding ofstream::open.

Instead couldn't you use the first suggestion in this post? Open the file as you normally would to get the correct permissions, and then construct an ofstream from the file descriptor.

Community
  • 1
  • 1
user786653
  • 26,194
  • 3
  • 39
  • 50
0
#include <iostream>
#include <fstream>

class gstream: public std::ofstream
{

void open(const std::string& filename, ios_base::openmode mode,int stuff)
        {
                //put stuff here

        }
};

int main() {

        gstream test;
//io stuff
return 0;
}

seems to work here.

marinara
  • 502
  • 1
  • 5
  • 10
0

Another option would be to create a wrapper class that contains an 'ofstream' object and has the interface you want, and passes the work onto its 'oftstream' member. It would look like this.

JohnMcG
  • 8,283
  • 5
  • 36
  • 49