2

Can anyone please tell my why this method won't compile?

void Statistics::readFromFile(string filename)
{
    string line;
    ifstream myfile (filename);
    if (myfile.is_open())
    {
        while (! myfile.eof() )
        {
            getline (myfile,line);
            cout << line << endl;
        }
        myfile.close();
    }

    else cout << "Unable to open file"; 

}

Should work, right? Yet, I always get the following error message:

Line Location Statistics.cpp:15: error:
   no matching function for call to
   'std::basic_ifstream<char, std::char_traits<char> >::
      basic_ifstream(std::string*)'

any help would be greatly appreciated.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
winsmith
  • 20,791
  • 9
  • 39
  • 49

5 Answers5

27
ifstream myfile (filename);

should be:

ifstream myfile (filename.c_str() );

Also, your read-loop logic is wrong. It should be:

while ( getline( myfile,line ) ){
   cout << line << endl;
}

The eof() function that you are using is only meaningful after you have tried to read read something.

To see why this makes a difference, consider the simple code:

int main() {
    string s; 
    while( ! cin.eof() ) {
        getline( cin, s );
        cout << "line is  "<< s << endl;
    }
}

If you run this and type ctrl-Z or ctrl-D to indicate EOF immediately, the cout will be performed even though no line has actually been input (because of the EOF). In general, the eof() function is not very useful, and you should instead test the return value of functions like getline() or the stream extraction operators.

8

Read the compiler error:

no matching function for call to 'std::basic_ifstream >::basic_ifstream(std::string*)

No matching function for call to: It can't find the function you're trying to call

std::basic_ifstream >:: - a member function of ifstream

:basic_ifstream(std::string*) - the constructor which takes a string pointer as its argument

So you try to create an ifstream by passing a string pointer to its constructor. And it can't find a constructor that accepts such an argument.

Since you're not passing a string pointer in the above, the code you've posted must be different from your actual code. Always copy/paste when asking about code. Typos make it impossible to figure out the problem. In any case, as I recall, the constructor does not accept a string argument, but only a const char*. So filename.c_str() should do the trick

Apart from that, you can do this a lot simpler:

ifstream myfile (filename);
    std::copy(std::istream_itrator<std::string>(myfile),
              std::istream_itrator<std::string>(),
              std::ostream_iterator<std::string>(std::cout));
}
jalf
  • 243,077
  • 51
  • 345
  • 550
3

The ifstream constructor has the following signature

explicit ifstream ( const char * filename, ios_base::openmode mode = ios_base::in );

You need to pass in a constant char* and a mode, for example:

ifstream ifs ( "test.txt" , ifstream::in );

The mode is optional, since it has a default value defined, so you can just use:

ifstream myfile ( filename.c_str() );
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
3

You should use fileName.c_str() so that you pass the const char* pointer to the myFile construction.

Naveen
  • 74,600
  • 47
  • 176
  • 233
0

The C++11 standard has resolved this defect. std::ifstream myfile(filename); should now compile, when filename has type std::string.