Using string variable as a file name

I want to use string variable as a file name like:

 
  string n = "file.txt"


How can i access the file by using "n" variable?


since open has prototype
void open (const char* filename, ios_base::openmode mode = ios_base::in);
you need to convert std::string to const char *
using const char* string::c_str() const;
The open() is a C-function. C++ has std::ifstream objects. See http://en.cppreference.com/w/cpp/io/basic_ifstream/basic_ifstream
i was talking about std::ifstream::open
http://www.cplusplus.com/reference/fstream/ifstream/open/
Since C++11 you can pass the std::string directly. Pre-C++11 you have to convert it a C string (char*) first, which you can use the c_str() function to do.
http://www.cplusplus.com/reference/string/string/c_str/
Topic archived. No new replies allowed.