尝试将字符串传递给std

尝试将字符串传递给std

本文介绍了尝试将字符串传递给std :: ifstream作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用从向量中检索的字符串将文件名传递给ifstream打开。

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 ifile(files[0] ,std::ios::binary);

编译时返回:

:没有匹配的函数调用'std :: basic_ifstream> :: basic_ifstream(std :: basic_string,std :: allocator>& const std :: _Ios_Openmode&)*

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

如果我尝试将文件[0]转换为const char *首先

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);

编译器返回:

'l'未在此范围内声明

error: 'l' was not declared in this scope

我在这里做错了什么?

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

推荐答案

尝试:

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

这篇关于尝试将字符串传递给std :: ifstream作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 18:04