472,778 Members | 2,519 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

how to print a filename from ifstream?

sam
Hi,

Can anyone tell me how to print a file name from ifstream?

the following cout code does not print the filename I created with
ifstream preivous:

ifstream is;
is.open ("text.txt");

cout << "filename in ifstream: " << is << endl;

Sam.
Jul 23 '05 #1
10 17288
Don't you think is a little bit stupid ?
If you know the name of a stream i design-time simply print it like
cout<<"name";

If you don't know it you must have it in a variable haven't you ?
In this case use cout<<variable;

--
SirMike
the code is my strength
http://www.sirmike.grudziadz.com
Jul 23 '05 #2
sam wrote:
Can anyone tell me how to print a file name from ifstream?
Nope. Nobody can. ifstream does not retain that information.
[..]


V
Jul 23 '05 #3
I don't know if it will help you,
but if it okfor you you can do like that:
String FileName;
FileName="text.txt";
cout <<"filename ..."<<FileName<<endl;
but it is the same like SirMike said to you

Jul 23 '05 #4

"sam" <sam++@--.com> wrote in message news:d5**********@news.hgc.com.hk...
Hi,

Can anyone tell me how to print a file name from ifstream?

the following cout code does not print the filename I created with
ifstream preivous:

ifstream is;
is.open ("text.txt");

cout << "filename in ifstream: " << is << endl;

Sam.


That doesn't make sense. The only way a file name would find itself in an
ifstream is if the filename was read / parsed from the specified file.

Don't you think that the input file stream should jealously protect the
provided filename parameter from modification during file stream processing
(even when the parameter is required to be a constant, such as in this
case)? Shouldn't this fact pop out as logicly obvious in any object that is,
or will be, processing a stream?

How would you feel if i was able to change your parachute to an umbrella
between the time you boarded the plane, jumped and hit the ground? Do you
beleive that a programmer doesn't have the responsability to protect that
parachute?

Don't laugh. The consequences of not protecting the ifstream's parameter
would be worse. Not because the ifstream never gets to hit the ground, but
because the whole world that is in (the program) enters a state of
unexpected behaviour (UB).

Have you ever heard of variables?

try:

#include <iostream>
#include <fstream>
#include <string>

int main()
{
std::string s_filename("text.txt");

std::ifstream ifs;
ifs.open(s_filename.c_str());
if (!ifs)
{
std::cout << "error while opening ";
std::cout << s_filename << std::endl;
}

std::cout << s_filename << std::endl;

return 0;
}
Jul 23 '05 #5

"codigo" <co****@codigo.trap.com> wrote in message
news:Kw*******************@news20.bellglobal.com.. .

"sam" <sam++@--.com> wrote in message news:d5**********@news.hgc.com.hk...
Hi,

Can anyone tell me how to print a file name from ifstream?

the following cout code does not print the filename I created with
ifstream preivous:

ifstream is;
is.open ("text.txt");

cout << "filename in ifstream: " << is << endl;

Sam.
That doesn't make sense. The only way a file name would find itself in an
ifstream is if the filename was read / parsed from the specified file.


Well, that's true, given the actual design of the filestream. But it's
certainly *concievable* to envision a filestream class design that
maintained the filename as a member which could be queried. Of course, that
would mean using something like "is.name", not just "is", but it's possible.
Don't you think that the input file stream should jealously protect the
provided filename parameter from modification during file stream
processing
(even when the parameter is required to be a constant, such as in this
case)? Shouldn't this fact pop out as logicly obvious in any object that
is,
or will be, processing a stream?

How would you feel if i was able to change your parachute to an umbrella
between the time you boarded the plane, jumped and hit the ground? Do you
beleive that a programmer doesn't have the responsability to protect that
parachute?

Don't laugh. The consequences of not protecting the ifstream's parameter
would be worse. Not because the ifstream never gets to hit the ground, but
because the whole world that is in (the program) enters a state of
unexpected behaviour (UB).


Well, that's a bit over-dramatic, in my opinion. Nobody said anything about
changing the name while the stream is open, did they? And even if one were
able to attempt to change the variable while the file was open, that doesn't
mean that the world would come crashing down. It *might* screw up a given
file system if the filename were changed while the file was open, but the OS
could (and likely does) forbid that anyway, which would mean that even if
the filestream class allowed the attempt to change it, the actual action
would fail in that instance, most likely with an exception of some sort,
which it could catch as needed.

-Howard

Jul 23 '05 #6

"Howard" <al*****@hotmail.com> wrote in message
news:lf*********************@bgtnsc05-news.ops.worldnet.att.net...

"codigo" <co****@codigo.trap.com> wrote in message
news:Kw*******************@news20.bellglobal.com.. .

"sam" <sam++@--.com> wrote in message news:d5**********@news.hgc.com.hk...
Hi,

Can anyone tell me how to print a file name from ifstream?

the following cout code does not print the filename I created with
ifstream preivous:

ifstream is;
is.open ("text.txt");

cout << "filename in ifstream: " << is << endl;

Sam.
That doesn't make sense. The only way a file name would find itself in an ifstream is if the filename was read / parsed from the specified file.


Well, that's true, given the actual design of the filestream. But it's
certainly *concievable* to envision a filestream class design that
maintained the filename as a member which could be queried. Of course,

that would mean using something like "is.name", not just "is", but it's possible.

Possible it certainly is, in fact its trivial. Would it be desireable,
probably not. The issue here is why provide access to a provided constant
string filename parameter? The OP can just as well pass a variable to the
ifstream and then manipulate the variable without bothering the ifstream or
object. I would think that efficiency is a primary concerns in the design of
such a mutating object.
Don't you think that the input file stream should jealously protect the
provided filename parameter from modification during file stream
processing
(even when the parameter is required to be a constant, such as in this
case)? Shouldn't this fact pop out as logicly obvious in any object that
is,
or will be, processing a stream?

How would you feel if i was able to change your parachute to an umbrella
between the time you boarded the plane, jumped and hit the ground? Do you beleive that a programmer doesn't have the responsability to protect that parachute?

Don't laugh. The consequences of not protecting the ifstream's parameter
would be worse. Not because the ifstream never gets to hit the ground, but because the whole world that is in (the program) enters a state of
unexpected behaviour (UB).

Well, that's a bit over-dramatic, in my opinion. Nobody said anything

about changing the name while the stream is open, did they? And even if one were able to attempt to change the variable while the file was open, that doesn't mean that the world would come crashing down. It *might* screw up a given
file system if the filename were changed while the file was open, but the OS could (and likely does) forbid that anyway, which would mean that even if
the filestream class allowed the attempt to change it, the actual action
would fail in that instance, most likely with an exception of some sort,
which it could catch as needed.

-Howard

Which brings us back to the crux of the problem. I can understand checking a
file stream's error condition bits, but why a *supplied* parameter?

#include <iostream>
#include <fstream>
#include <string>

class FileStream
{
const std::string m_filename;
std::ifstream m_ifs;
std::string m_sdata;
public:
FileStream(std::string s) : m_filename(s), m_ifs(), m_sdata("") { }
~FileStream() { }
std::string getFilename() const
{
return m_filename;
}
const std::string getData()
{
m_ifs.open(m_filename.c_str());
if (!m_ifs)
{
m_sdata = "error opening file " + m_filename;
return m_sdata;
}
std::getline(m_ifs, m_sdata);
return m_sdata;
}
};

int main()
{
std::string s("data.dat");
FileStream fs(s);

std::cout << fs.getFilename() << std::endl;
std::cout << fs.getData();

return 0;
}

output:
data.dat
the only data string

whats wrong with...

int main()
{
std::string s("data.dat");
FileStream fs(s);

std::cout << s << std::endl;
std::cout << fs.getData();

return 0;
}

and forego having to provide the getFilename() member function?

Jul 23 '05 #7
LOL

Jul 23 '05 #8
I didn't check, but I strongly believe that implementations of
std::fstream do not store name of the opened file as private member.
They just pass it over to the os (when you call open or use appropriate
constructor) and os then returns some sort of specific
handler/descriptor which is stored.

Jul 23 '05 #9

"__PPS__" <bl******@mail.ru> wrote in message
news:11********************@g14g2000cwa.googlegrou ps.com...
I didn't check, but I strongly believe that implementations of
std::fstream do not store name of the opened file as private member.
They just pass it over to the os (when you call open or use appropriate
constructor) and os then returns some sort of specific
handler/descriptor which is stored.


It doesn't store the filename on those implementations i've used. My
arguement was that providing such a mechanism just doesn't make sense. Even
in a class with an ifstream member.

Look at the OP's code. He's passing a constant string to the ifs instead of
storing the string for later use.
Jul 23 '05 #10

"codigo" <co****@codigo.trap.com> wrote in message
news:l0********************@news20.bellglobal.com. ..

"__PPS__" <bl******@mail.ru> wrote in message
news:11********************@g14g2000cwa.googlegrou ps.com...
I didn't check, but I strongly believe that implementations of
std::fstream do not store name of the opened file as private member.
They just pass it over to the os (when you call open or use appropriate
constructor) and os then returns some sort of specific
handler/descriptor which is stored.


It doesn't store the filename on those implementations i've used. My
arguement was that providing such a mechanism just doesn't make sense.
Even
in a class with an ifstream member.

Look at the OP's code. He's passing a constant string to the ifs instead
of
storing the string for later use.


Yes, I see that, and you're right, that the calling code can store that
value itself. But that's a simple case. Lots of objects are created that
store the values passed to their constructors. Why not be able to query
that information later? Suppose you had a pool of such objects. Why store
the parameters passed to each object's constructor yourself, when the
obejcts themselves can store it and you can query them later. I'll admit
that's not so likely with streams (I can't think of a major use for a pool
of streams). But in the general case, it makes perfect sense to query an
object for information passed to its constructor. That was my point.

-Howard

Jul 23 '05 #11

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

Similar topics

11
by: hokiegal99 | last post by:
How would I determine if a filename is greater than a certain number of characters and then truncate it to that number? For example a file named XXXXXXXXX.txt would become XXXXXX fname = files...
7
by: Anton Ishmurzin | last post by:
Greetings All, I think everybodyknows the answer already. But i am quite a newbie in c++. I've got the following line in my code: ifstream ini_file_in("filename.dat", ios::in); But, the...
6
by: Ram Laxman | last post by:
Iam new bie to C++ programming.. I want to write a program which will read the Comma separated values(CSV) file column wise. For example: In a.txt: "TicketNumber","Phone","CarNumber"...
12
by: Steven T. Hatton | last post by:
I know of a least one person who believes std::ifstream::read() and std::ofstream::write() are "mistakes". They seem to do the job I want done. What's wrong with them. This is the code I...
1
by: chatmili | last post by:
I am very frustrated after searching for three days. My problem seems to be so hard that no one really had an answer for it. I have seen postings with similar problem but they were either ignored...
0
by: MadCrazyNewbie | last post by:
Hi Group, I have the following code, but not sure if this is write, I would like to be able with the View code, for the document to be opened eg. C:\test.Doc and with the PRint button for it to...
3
beacon
by: beacon | last post by:
Hi, I'm doing a homework assignment, but I have done most of the work and am only looking for some tutoring to get past this small portion of the program I have been tasked to write. The...
11
by: adramolek | last post by:
So... I'm trying to get used to using C++ ifstream (or ofstream) instead of stdio (although I'm having second thoughts). Anyways, I want to be able to display a meaningful error message if ifstream...
66
by: happyse27 | last post by:
Hi All, my html code is sno 1) and perl code is sno 2). a) I tried to print $filename and it cant print out the value, only blank was displayed, and the file could not be uploaded. And it...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.