Click to See Complete Forum and Search --> : Stream types


grahamr (work)
February 27th, 2008, 10:21 AM
Is it possible to get the filename from a stream?

My class has access to a ofstream that another class creates but I need to create an ifstream that has access to that file also. To pass the filename to the class it would mean modifying half a dozen classes just to pass the filename. I was hoping I wouldn't have to do that, but can't see a way looking at the fstream header of initializing from another stream or retrieving the original filename.

G.

PredicateNormative
February 27th, 2008, 04:03 PM
I don't know of any way to do this. That said, I must admit, where possible I wrap ofstream and ifstream objects in RAII classes, and give these classes the ability to return the filename that they have been initialised with. I don't know if this is possible in your situation but, if you are going to make a change to the interfaces anyway it might possibly be worth considering wrapping the ofstream object so that you don't have to pass multiple file related objects at each interface? There is also the increased exception saftey of a properly implemented RAII wrapper.

grahamr (work)
February 28th, 2008, 08:21 AM
I had considered wrapping it in a class but thought it was a bit overkill just to get the filename back. I hadn't planned on doing anything else with it.

Probably will go with that.