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;
}
How do i use string filename as myfile.open(filename)
Initially i was using file.txt instead, but if i use a variable that pass in by the function, like string filename, it give me an error
myfile.open("file.txt");
Error message is as followed:
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
The constructor for
std::ifstream::open(for the particular standard of C++ that you’re using) doesn’t allow astd::stringargument, so you have to use:The constructor expects type
const char *which you can obtain from astd::stringobject using itsc_str()member function.