Dynamically Loading the Address of an ifstream(using pointers)

Hello,

I just made a program to count the lines of a text document, the address of which is in the actual code. Now its my goal to have the user input the address and count the lines of that document. I tried to do this using a pointer but it is throwing an error:
"no matching function for call to 'std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&, const std::_Ios_Openmode&)'|"

Here is my 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
#include <iostream>
#include <fstream>

using namespace std;

int i = 1;
string docLocation = "C:\\...\\test.txt";

int main()
{
    const string * pString = &docLocation;
    cout << "Input the location of your text document:";
    cin >> docLocation;
    cout << endl;

    ifstream myInputFileStream(*pString , ifstream::in);

    char ch;
    while( !myInputFileStream.eof() ) {
         myInputFileStream.get(ch);
         if (ch == '\n'){
             i++;
         }
         if( i > 300){
             cout << "THEY CAME FROM BE-BEHIND!";
             break;            }
        }
     myInputFileStream.close();
     cout << "Number of lines in the text document: " << i << endl;
}


Any help is appreciated!
Thanks in advance
-GS333
Before C++11 it was not possible to pass std::string to std::ifstream. It had to be a C string (char*). std::string has a member function c_str() that returns a C string that you can use.
ifstream myInputFileStream(pString->c_str() , ifstream::in);
Last edited on
Thank you so much! Worked like a charm. :)
Topic archived. No new replies allowed.