void searchString(const string selection,const string filename)
{
ifstream myfile;
string sline;
string sdata;
myfile.open(filename);
while(!myfile.eof())
{
getline(myfile,sline);
sdata = sdata + sline;
}
我如何使用字符串文件名作为 myfile.open(filename)
最初我使用的是file.txt,但如果我使用函数传入的变量,如字符串文件名,它会给我一个错误
myfile.open("file.txt");
错误信息如下:
main.cpp:203:25: error: no matching function for call to ‘std::basic_ifstream<char>::open(const string&)’
main.cpp:203:25: note: candidate is:
/usr/include/c++/4.6/fstream:531:7: note: void std::basic_ifstream<_CharT, _Traits>::open(const char*, std::ios_base::openmode) [with _CharT = char, _Traits = std::char_traits<char>, std::ios_base::openmode = std::_Ios_Openmode]
/usr/include/c++/4.6/fstream:531:7: note: no known conversion for argument 1 from ‘const string {aka const std::basic_string<char>}’ to ‘const char*’
make: *** [main.o] Error 1