c++ getting the filename and path of an ofstream object

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zmohamed
    New Member
    • Apr 2010
    • 1

    c++ getting the filename and path of an ofstream object

    I'm passing an ofstream object to a function within my program. In that function, I'd like to extract the path and filename of the ofstream object, modify the filename only, create a new ofstream object using the old path and new filename and write something to it.

    How do I get the path and filename of a passed ofstream object?

    Code:
    std::ostream& Func::writeOut(std::ostream& out)
    {
       std::string sPath = getPath(out)     // how do I implement this?
       std::string sFile = sPath + "//NewFile";
    
       std::ofstream oFile(sFile, std::ios::out);
    
       sFile << "Write something" << std::endl;
    
       return(out);
    }
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    You can't because ostream may be e.g. ostringstream that has no file associated with it.

    Comment

    Working...