no matching function for call to `getline(std::ifstream&, char*&)'

Mar 16, 2013 at 8:07pm
Hello, I'm new. I read the C++ tutorial, and I'm trying to make a test program where you type a file path and it prints out the text inside it.

I've got a few problems, I could solve them all, but I still have a problem and I searched through all the ways, and I could not find an answer.

20 C:\[path]\test.cpp no matching function for call to `getline(std::ifstream&, char*&)' 


So here's my test code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
using namespace std;

int main() {
    char* path;
    char* currentline;
    cout << "File path:" << endl;
    cin >> path;
    cout << "Reading:" << path << endl;
    system ("ping localhost -n 3 >nul");
    system ("cls");
    ifstream fileopen;
    fileopen.open (path);
    if (fileopen.is_open())
    {
         do {
             getline (fileopen,currentline);
             cout << currentline << endl;
         } while (fileopen.good());
         fileopen.close();
    }
    
    else {
         cout << "Could not read file, probably it does not exist." << endl;
    }
    cout << "---" << endl;
    cout << "Press any key to exit." << endl;
    system ("pause >nul");
    return 0;
}


The "wrong line" is marked in bold (line 20). I really don't know what I'm doing wrong...

And one last thing.. remember I'm new :P
Last edited on Mar 16, 2013 at 8:30pm
Mar 16, 2013 at 8:13pm
The first "wrong line" is line 11. path points to some random place in memory. You're writing to that random place on line 11. You would have the same problem on line 20. currentline is pointing to some random place in memory.

Changing path and currentline to std::string types would be a step in the right direction.
Mar 16, 2013 at 8:19pm
16 C:\[Path]\test.cpp no matching function for call to `std::basic_ifstream<char, std::char_traits<char> >::open(std::string&)' 


Now I'm getting this.. I have no idea on what to do :x
Mar 16, 2013 at 8:24pm
Depending on the age of the compiler, you may need to do this:
 
    fileopen.open (path.c_str());
Mar 16, 2013 at 8:28pm
Thank you, it works now.
Topic archived. No new replies allowed.