Thunfische
Thunfische

Reputation: 1157

Accessing a text file with a variable

Is there any way where i can access? There are text files like john.txt micheal.txt james.txt ....

I can access them with this code :

ifstream file1( "james.txt", ios::in );

Can I open a file like?

string name = "james"; ifstream file1( name, ios::in )

Upvotes: 0

Views: 87

Answers (2)

Mohammad Javad Naderi
Mohammad Javad Naderi

Reputation: 506

Try this:

char name[]="james.txt";
ifstream file1( name, ios::in );

Upvotes: -1

Rémi
Rémi

Reputation: 3744

maybe you can try something like:

ifstream file1((name + ".txt").c_str(), ios::in);

Upvotes: 4

Related Questions