Is there an easy way to open an fstream using a string? My code is as follows:

bool openArray(std::string *arrayFileName, int *width, int *height,int *myArray)
{
      std::fstream BoardFile;
      BoardFile.open(*arrayFileName);

I obviously cannot use this method since *arrayFileName is not char* but is there another way to do it?

Recommended Answers

All 2 Replies

Is there an easy way to open an fstream using a string? My code is as follows:

bool openArray(std::string *arrayFileName, int *width, int *height,int *myArray)
{
      std::fstream BoardFile;
      BoardFile.open(*arrayFileName);

I obviously cannot use this method since *arrayFileName is not char* but is there another way to do it?

Well you can use the c_str () command:
http://www.cplusplus.com/reference/string/string/c_str.html

string filename = "file.txt";
ifstream ins;
ins.open (filename.c_str ());
std::fstream BoardFile;
      BoardFile.open(arrayFileName->c_str());
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.