Ofstream filename variable

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cpp

    Ofstream filename variable

    When I create an instance of ofstream, what is the name of the member
    variable that holds the filename?
    For example:

    ofstream ofs("Output.txt ");
    cout << ofs.WhatIsThePa thVariable;

    If there isn't a public member variable I can use, then is there at least a
    function that displays the filename?

    Thanks
  • John Harrison

    #2
    Re: Ofstream filename variable


    "cpp" <cpp@on.the.net > wrote in message
    news:Xns949D71B 41BD7Dcpponthen et@24.24.2.165. ..[color=blue]
    > When I create an instance of ofstream, what is the name of the member
    > variable that holds the filename?[/color]

    There is none.
    [color=blue]
    > For example:
    >
    > ofstream ofs("Output.txt ");
    > cout << ofs.WhatIsThePa thVariable;
    >
    > If there isn't a public member variable I can use, then is there at least[/color]
    a[color=blue]
    > function that displays the filename?
    >[/color]

    No again.

    Use a little imagination. Since you opened the file with a file name you
    must also be capable of storing that file name somewhere. For instance you
    could derive a class from ofstream that stores the filename

    class ofstream_with_f ilename : public ofstream
    {
    public:
    void open(const char* fn)
    {
    ofstream::open( fn);
    filename = fn;
    }
    string get_filename() const { return filename; }
    private:
    string filename;
    };

    I hope that gives you the idea, real code would be a little more
    sophisticated than the above.

    john


    Comment

    • RCS

      #3
      Re: Ofstream filename variable

      John Harrison wrote:[color=blue]
      > class ofstream_with_f ilename : public ofstream
      > {
      > public:
      > void open(const char* fn)
      > {
      > ofstream::open( fn);
      > filename = fn;
      > }
      > string get_filename() const { return filename; }
      > private:
      > string filename;
      > };
      >
      > I hope that gives you the idea, real code would be a little more
      > sophisticated than the above.
      >
      > john[/color]

      Yes, REAL CODE(tm) would do it like this:

      string& get_filename() const { return filename; }

      Apart from this, why do you say that "real code" would do it so much
      different than your snippet?

      Just wondering.

      RXX

      Comment

      • Duane Hebert

        #4
        Re: Ofstream filename variable


        "RCS" <rcs333@online. no> wrote in message news:n4a0c.7274 $rj4.98132@news 2.e.nsc.no...[color=blue]
        > Yes, REAL CODE(tm) would do it like this:
        >
        > string& get_filename() const { return filename; }[/color]

        So real code would return a non const reference to
        a private member?

        How about
        const string & get_filename() const { return filename;}


        Comment

        • John Harrison

          #5
          Re: Ofstream filename variable


          "RCS" <rcs333@online. no> wrote in message
          news:n4a0c.7274 $rj4.98132@news 2.e.nsc.no...[color=blue]
          > John Harrison wrote:[color=green]
          > > class ofstream_with_f ilename : public ofstream
          > > {
          > > public:
          > > void open(const char* fn)
          > > {
          > > ofstream::open( fn);
          > > filename = fn;
          > > }
          > > string get_filename() const { return filename; }
          > > private:
          > > string filename;
          > > };
          > >
          > > I hope that gives you the idea, real code would be a little more
          > > sophisticated than the above.
          > >
          > > john[/color]
          >
          > Yes, REAL CODE(tm) would do it like this:
          >
          > string& get_filename() const { return filename; }[/color]

          No! Three things wrong with that

          1) Doesn't compile (non-const reference being taken to const object).
          2) Returning reference to internal data member is bad style generally
          because it limits your implementation. Suppose you were porting this code to
          a O/S where you did have the filename available from some O/S API. How could
          you take advantage of that given that you are returning a reference?
          3) Most importantly, it's useless functionality. Why would you want to
          change the file anme associated with an open file? By return a non-const
          reference you are allowing the user to do exactly that.

          [color=blue]
          >
          > Apart from this, why do you say that "real code" would do it so much
          > different than your snippet?
          >[/color]

          I was thinking of two things, implementing all of ofstream constructors in
          ofstream_with_f ilename, and error handling, what do you do with the filename
          if the open fails (leave it unset presumably). But I was too lazy to
          actaully implement that.
          [color=blue]
          > Just wondering.
          >
          > RXX
          >[/color]

          john


          Comment

          • Chris Mantoulidis

            #6
            Re: Ofstream filename variable

            RCS <rcs333@online. no> wrote in message news:<n4a0c.727 4$rj4.98132@new s2.e.nsc.no>...[color=blue]
            > John Harrison wrote:[color=green]
            > > class ofstream_with_f ilename : public ofstream
            > > {
            > > public:
            > > void open(const char* fn)
            > > {
            > > ofstream::open( fn);
            > > filename = fn;
            > > }
            > > string get_filename() const { return filename; }
            > > private:
            > > string filename;
            > > };
            > >
            > > I hope that gives you the idea, real code would be a little more
            > > sophisticated than the above.
            > >
            > > john[/color][/color]
            [color=blue]
            > Yes, REAL CODE(tm) would do it like this:
            >
            > string& get_filename() const { return filename; }
            >
            > Apart from this, why do you say that "real code" would do it so much
            > different than your snippet?
            >
            > Just wondering.
            >
            > RXX[/color]

            REAL CODE(tm) would do it like this:

            const string& get_filename() const { return filename; }

            or else the following would have been possible (with your piece of REAL CODE):

            ofstream_with_f ilename owf;
            owf.open("test. txt");
            owf.get_filenam e() = "something" ;

            and we don't want the last to happen now do we? ;)

            Comment

            Working...