Opening a file using a variable

Hello,

I know this question has been asked a million times, and I've spend a lot of time reading answers on forums, but for whatever reason I can't get this to work.

I can open a file for reading if I explicitly give the name:

 
indata.open("image1.png");


However, I want to use a variable so that I can open "image1.png", "image2.png", etc.

The problem is that I can't find a way to convert my string variable to a char. For example, I've tried:

 
char fileName = (testName.c_str());


The resulting error says that I'm trying to set a const char to a char.

Please help before I throw my laptop against the wall!

-John
I'm trying to set a const char to a char.

Actually you're trying to set a const char* to a char.

This would work:

1
2
const char* fileName = testName.c_str();
indata.open(fileName);

Or simply this:

indata.open( fileName.c_str() );

PS Dream Theater rocks
Thank you very much! Always cool to run into another Dream Theater fan (99% of people don't know who Petrucci is).

Couple more questions:

1) What does the * mean in const char*?
2) Since I'm using a const variable, is there a way to change the variable in a loop? I'm assuming I can't assign the variable again in a for loop, but can I define the variable inside the loop?

Thanks again.

-John
Topic archived. No new replies allowed.