Thread: function access class ofstream object

  1. #1
    Registered User
    Join Date
    May 2016
    Posts
    40

    function access class ofstream object

    (I'm a newb learner) I've created the smallest possible app (below) to depict my issue which is accessing a class member ofstream object(s) from within class functions. My actual app has too many functions accessing file(s) that it doesn't really lend itself to creating an ofstream friend operator for each scenario. And I always try to stay away from global items.
    So I have two specific questions,
    (1) Please look at the below code and tell me why the 2nd function with the this-> ptr scenario compiles with no errors but doesn't work. No file is created or written.
    (2) Please give input on any better or other ways (than the 1st function scenario) to solve this scope access issue, other than obviously creating, opening and re-opening the file object(s) repetitively from each function.

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <sstream>
    #include <fstream>
    #include <cstdlib>
    
    using namespace std;
    
    class Base{
     public:
     // ofstream objects
     ofstream DrawDn_File;
     ofstream ReVest_File;
    
     // Class Funcs
     int BaseScope1(ofstream& rFile);
     int BaseScope2();
    
     //Ctor
     Base() {};
     //Dtor
     ~Base(){};
    };  // End Class declaration
    
    //This Func opens and writes the file
    int Base::BaseScope1(ofstream& rFile)
    {
      rFile.open("DrawDown.txt");
      rFile << "writing to DrawDown.txt from Scope1 func" << endl;
      cout << "Thru BaseScope1 Func" << endl;
      return 0;
    }
    
    //This Func neither creates or writes the file
    int Base::BaseScope2()
    {
      this->DrawDn_File.open("ReVest.txt");  // default overwrite
      this->DrawDn_File << "writing to ReVest.txt from Scope2 func" << endl;
      cout << "Thru BaseScope2 Func" << endl;
      return 0;
    }
    
    int main()
    {
      Base BaseClass;
      BaseClass.BaseScope1(BaseClass.DrawDn_File);
      BaseClass.BaseScope2();
      return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    tell me why the 2nd function with the this-> ptr scenario compiles with no errors but doesn't work. No file is created or written.
    The problem is being caused because you fail to close the stream between calls.

    Why are you using stream instances in the class instead of just creating the stream in the function?

    My actual app has too many functions accessing file(s) that it doesn't really lend itself to creating an ofstream friend operator for each scenario.
    Then perhaps your class needs to be redesigned.


    Jim

  3. #3
    Guest
    Guest
    You pass BaseClass's DrawDn_File member to your method but you never close it.

    If on line 32 you called rFile.close(), it should work.

    p.s. This seems like a really contrived setup – I hope just doing things this way for the sake of testing.

  4. #4
    Registered User
    Join Date
    May 2016
    Posts
    40
    Quote Originally Posted by jimblumberg View Post
    The problem is being caused because you fail to close the stream between calls.

    Why are you using stream instances in the class instead of just creating the stream in the function?
    Jim
    Quote Originally Posted by Guest View Post
    You pass BaseClass's DrawDn_File member to your method but you never close it.

    If on line 32 you called rFile.close(), it should work.

    p.s. This seems like a really contrived setup – I hope just doing things this way for the sake of testing.
    Thanks for replies,
    (1) ok I was thinking when the function went out of scope the ofstream object closed automatically, but I surmise now that since it's instanced as a class member that once opened it stays open till closed. I.e. I was opening it twice (?) if that's possible.

    (2) I could very well create the stream every single time in the function, it would appear only minimal more typing and I assume same or less overhead (?).
    Was just thinking create it once accessible from all functions would be efficient, but I gather not the case (?).

  5. #5
    Guest
    Guest
    (1) The object is not being constructed inside BaseScope1, lives outside of it, and thus won't get destructed/closed when the function returns.

    (2) The BaseScope2 way is fine – I was merely alluding what you attempted with BaseScope1, which is weird. Just be sure to explicitly close the file if you want to open other files using the same ofstream object.
    Last edited by Guest; 07-06-2016 at 12:36 PM.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    but I surmise now that since it's instanced as a class member that once opened it stays open till closed.
    Or the class goes out of scope.

    I was opening it twice (?) if that's possible.
    A single stream can only be associated with one file at time.

    I could very well create the stream every single time in the function, it would appear only minimal more typing
    The amount of typing should never be a concern, clear, easy to read programs that function properly are more important than a few extra characters being typed.

    and I assume same or less overhead
    Opening and closing files multiple times has more overhead than keeping the stream open. But IMO if you have the instance as a member variable the class should open the file rather than relying on some outside source to open the file.

    You should also always check that the file opens correctly.

    Jim

  7. #7
    Registered User
    Join Date
    May 2016
    Posts
    40
    Quote Originally Posted by Guest View Post
    (1) The object is not being constructed inside BaseScope1, lives outside of it, and thus won't get destructed/closed when the function returns.

    (2) The BaseScope2 way is fine – I was merely alluding what you attempted with BaseScope1, which is weird. Just be sure to explicitly close the file if you want to open other files using the same ofstream object.
    Thanks, I can see (now) why you say that. After working with the code I discovered I did not need the this-> ptr. I must have originally had a spelling error since I got a compiler scope error at first, which prompted me to add the this->.
    I somehow corrected the spelling though or the this-> would not have done anything. In any case it works now without the this, and in essence I don't have a scope issue at all. What a foo bar I must have done.


    Quote Originally Posted by jimblumberg View Post
    . . . . . . .
    . . . . . . .
    Opening and closing files multiple times has more overhead than keeping the stream open. But IMO if you have the instance as a member variable the class should open the file rather than relying on some outside source to open the file.

    You should also always check that the file opens correctly.

    Jim
    Thanks Jim

    I apologize to everyone for not checking my spelling more or even pasting to make sure. I honestly don't remember exactly the happenstance that transpired, but it would appear I had a spelling error causing the "whatever does not have scope in this function" error and then the rest was my own foo bar. If I had spotted that I probably would not even needed to post at all.
    But I have learned from your input none the less.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create a new class object through a function
    By Darkroman in forum C++ Programming
    Replies: 9
    Last Post: 03-06-2012, 08:10 AM
  2. how to receive a class object in a function?
    By nyy601 in forum C++ Programming
    Replies: 5
    Last Post: 02-28-2011, 01:55 AM
  3. how to receive a class object in a function?
    By nyy601 in forum C Programming
    Replies: 1
    Last Post: 02-26-2011, 01:07 AM
  4. Passing an array of a class object into a function
    By maoqiu in forum C++ Programming
    Replies: 3
    Last Post: 10-25-2007, 08:42 AM
  5. Passing ofstream object to another function
    By otika in forum C++ Programming
    Replies: 2
    Last Post: 07-24-2006, 09:28 PM

Tags for this Thread