What is the "something" above?Code:ifstream file_input("myinputfile.txt");
cout << file_input.something; // prints "myinputfile.txt"
Printable View
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"
}
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;
}
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.