开发者

Attempting to pass string to std::ifstream as argument [duplicate]

开发者 https://www.devze.com 2023-02-13 08:43 出处:网络
This question already has an answer here: No matching function - ifstream open() (1 answer) Closed 7 years ago.
This question already has an answer here: No matching function - ifstream open() (1 answer) Closed 7 years ago.

I'm attempting to use a string retrieved from a vector to pass a filename to ifstream for opening.

vector<string> files;

//Populate vector

std::ifstream if开发者_如何学运维ile(files[0] ,std::ios::binary);

When compiled This returns:

error: no matching function for call to 'std::basic_ifstream >::basic_ifstream(std::basic_string, std::allocator >&, const std::_Ios_Openmode&)*

If I attempt to convert files[0] to const char * first

vector<string> files;

std::string l = files[0];
const char *p;
p = l.c_str();
std::ifstream ifile(p ,std::ios::binary);

The compiler returns:

error: 'l' was not declared in this scope

What am I doing wrong here? It's something basic and glaring i'm sure.


Try this:

std::ifstream ifile(files[0].c_str(), std::ios::binary);
0

精彩评论

暂无评论...
验证码 换一张
取 消