winsmith
winsmith

Reputation: 21602

Spot the error in this file reading code (C++)

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.

Upvotes: 2

Views: 5694

Answers (5)

user1084944
user1084944

Reputation:

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

Upvotes: 0

anon
anon

Reputation:

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.

Upvotes: 27

Stack Overflow is garbage
Stack Overflow is garbage

Reputation: 248129

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));
}

Upvotes: 8

Bill the Lizard
Bill the Lizard

Reputation: 405955

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() );

Upvotes: 3

Naveen
Naveen

Reputation: 73473

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

Upvotes: 3

Related Questions