Hi,

I assigned the name of the file to string vector.

I mean when I print the vector, it seems like

cout<< files[0];

It prints file.txt

and I want to open the file by using vector.

I tried ifstream file ("files[0]"); and ifstream file (files[0]);

but they did not work. Please help me out !

Recommended Answers

Presumably you're using a vector of std::string. The fstream constructors don't have an overload for std::string, so you need to call c_str():

ifstream file(files[0].c_str());
Jump to Post

All 3 Replies

Are you sure the file exists and is in the correct folder? What do you mean by saying they do not work?
Are you getting compilation errors, or is it something else? It would really help if you posted your code.

Presumably you're using a vector of std::string. The fstream constructors don't have an overload for std::string, so you need to call c_str():

ifstream file(files[0].c_str());

ok thank you very much it is solved ;)

Be a part of the DaniWeb community

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