Connecting Tech Pros Worldwide Forums | Help | Site Map

File associated with fstream object?

Sanyi Benczik
Guest
 
Posts: n/a
#1: Jul 22 '05
Is there a way to obtain the name of the file associated with a fstream
object?

For example if I have

using namespace std;
ofstream file("test.txt");

is there a function which evaluates to "test.txt"?

Thx,
Sanyi



Jack Klein
Guest
 
Posts: n/a
#2: Jul 22 '05

re: File associated with fstream object?


On Tue, 20 Jan 2004 01:49:44 -0500, Sanyi Benczik
<sbenczik@hotmail.com> wrote in comp.lang.c++:
[color=blue]
> Is there a way to obtain the name of the file associated with a fstream
> object?
>
> For example if I have
>
> using namespace std;
> ofstream file("test.txt");
>
> is there a function which evaluates to "test.txt"?
>
> Thx,
> Sanyi[/color]

There is no way to do this using standard C++. There might or might
not be a non-standard platform specific extension provided by your
compiler/OS combination. On some platforms, a single file may have
more than one name.

Of course you could always build an object that contained a
std::string with the name and a stream reference.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dietmar Kuehl
Guest
 
Posts: n/a
#3: Jul 22 '05

re: File associated with fstream object?


Jack Klein <jackklein@spamcop.net> wrote:[color=blue]
> On some platforms, a single file may have
> more than one name.[/color]

.... or none at all:

std::ofstream tmpfile("mytempfile");
std::remove("mytempfile");

This is fine eg. on POSIX platforms and, except for the use of the hardcoded
name, a pretty common technique to create temporary files which are to be
removed when the program dies for whatever reason.
[color=blue]
> Of course you could always build an object that contained a
> std::string with the name and a stream reference.[/color]

.... or attach the name to your file object:

int const index = std::ios_base::xalloc();
std::string filename("file.name");
std::ofstream file(filename.c_str());
file.pword(index) = const_cast<char*>(filename.c_str());
// ...
char const* name = static_cast<char*>(file.pword(index));
--
<mailto:dietmar_kuehl@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
Closed Thread