What is the "something" above?Code:ifstream file_input("myinputfile.txt"); cout << file_input.something; // prints "myinputfile.txt"
This is a discussion on Very Easy Question within the C++ Programming forums, part of the General Programming Boards category; Code: ifstream file_input("myinputfile.txt"); cout << file_input. something ; // prints "myinputfile.txt" What is the "something" above?...
What is the "something" above?Code:ifstream file_input("myinputfile.txt"); cout << file_input.something; // prints "myinputfile.txt"
Like this?
Or what is your question? I don't think ifstream stores the name of the file if that's what you mean. If you opened the file, the name was available to you - why can't you use that?Code:#include <iostream> #include <string> struct ifstream { std::string something; ifstream(const std::string& sth): something(sth) {} }; using std::cout; int main(){ ifstream file_input("myinputfile.txt"); cout << file_input.something; // prints "myinputfile.txt" }
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
If it is a command line argument then it is stored in a variable? Why can't you use that?
Code:string name = "somefile.txt"; ifstream fin(name.c_str()); if (!fin) { cout << "Couldn't open " << name; }
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
You're specifying the filename when you open the file, so you must know it.
If it can't be opened, just print that filename.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^