473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

File associated with fstream object?

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
Jul 22 '05 #1
2 1436
On Tue, 20 Jan 2004 01:49:44 -0500, Sanyi Benczik
<sb******@hotmail.com> wrote in comp.lang.c++:
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


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
Jul 22 '05 #2
Jack Klein <ja*******@spamcop.net> wrote:
On some platforms, a single file may have
more than one name.
.... 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.
Of course you could always build an object that contained a
std::string with the name and a stream reference.


.... 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:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
Phaidros eaSE - Easy Software Engineering: <http://www.phaidros.com/>
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
6
by: radnoraj | last post by:
Hi, I am sucessfull in redirecting console output to a file. but in this case nothing is displayed on the console, cout output is written to file without display. how do write the output to...
1
by: lkr | last post by:
hi i got one file transfer program using serialization which has a limitation that i can send only 8192 bytes(8KB). i want to send more than that wht can i do. how can i divide the file into...
0
by: Ramsey | last post by:
I have a unmanaged lib file that creates an fstream object. This lib file is used by a managed dll. Unfortunately when I run the code it crashes when I create the fstream object. I have a...
11
by: Dorsa | last post by:
HI, Could you please tell me the error in here. I am trying to open an XML file from a link. Response.Clear() Response.Expires = 0 Response.BufferOutput = False Response.ContentType =...
1
emaghero
by: emaghero | last post by:
Greetings and Salutations, I have started using the fstream class in C++ to create text files for input and output. Whenever I try to instantiate an fstream object using the following code ...
1
by: dwaterpolo | last post by:
Hi Everyone, I am trying to read two text files swY40p10t3ctw45.col.txt and solution.txt and compare them, the first text file has a bunch of values listed like: y y y y y y y
2
by: Rene | last post by:
Hello to all! I am a newbee to C++, I did a beginners' course, and now I am stuck with a strange problem. I have written the function that is pasted downwards. The peculiar thing is that when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.