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. 10 17749
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
sam wrote: Can anyone tell me how to print a file name from ifstream?
Nope. Nobody can. ifstream does not retain that information.
[..]
V
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
"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;
}
"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
"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?
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.
"__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.
"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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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"...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |