I have some text files in a folder named foo1, foo2,…,foo5. I tried to write a C++ program
to print out the contents of the files but the compiler is giving an error.
Here is the program.
int main(int argc, char *argv[])
{
string common="foo";
for(int count=1;count<=5;++count)
{
//Convert count to an string.
stringstream ss;
ss<<count;
string numstring=ss.str();
string filename=common+numstring;
ifstream infile(filename);
string line;
//Print out the lines from the file.
while(getline(infile,line))
{
cout<<line<<endl;
}
}
return 0;
}
The compiler is giving an error like
g++ -Wall c++.cpp
c++.cpp: In function ‘int main(int, char**)’:
c++.cpp:26: error: no matching function for call to ‘std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(std::string&)’
/usr/include/c++/4.4/fstream:454: note: candidates are: std::basic_ifstream<_CharT, _Traits>::basic_ifstream(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/fstream:440: note: std::basic_ifstream<_CharT, _Traits>::basic_ifstream() [with _CharT = char, _Traits = std::char_traits<char>]
/usr/include/c++/4.4/iosfwd:81: note: std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_ifstream<char, std::char_traits<char> >&)
Can someone help me out with this? The output of the program should be what I would get if I typed cat foo* at the terminal.
Replace:
with
The error message that you posted shows you this.
fstreamis a typedef forbasic_fstream<>. Your error message says, in essence, that there is nofstreamconstructor that takes astring&, but there is one that takes achar*.