tags:

views:

569

answers:

4

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.

+19  A: 
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.

anon
AAaaaaahh okay! Thanks a bunch!
winsmith
Don't you need an open mode?
Bill the Lizard
@Bill seems you dont. @Neil but won't eof() simply return false the first time? Because, it works.
winsmith
@bill nope - ifstreams are opened for reading - the "i" stands for input
anon
@Neil: Right, my ignorance. In the doc I linked to it even shows there's a default value of ios_base::in. Duh. :)
Bill the Lizard
+2  A: 

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
+3  A: 

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

Naveen
+7  A: 

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