"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;
}