cannot convert ‘std::string’ to ‘char**’ for argument ‘1’ to ‘__ssize_t getline(char**, size_t*, FILE*)’

You have quite a number of problems with your code.

Let’s start with:

int main(int argc, char **argv)
{
    int spaces,line,characters = 0; // Delete this line
    ifstream infile;                // Delete this line
    infile.open("fox.txt");         // Delete this line
    try{
        int result1 = count1(argv[1]);
        int result2 = count2(argv[1]);
        int result3 = count3(argv[1]);
        cout<<result1<<' '<<result2<<' '<<result3<<endl;
    } catch (const char *e) {
        cout<<e<<endl;
    }
}

All these are not used in main()! It seems you have a problem understanding the scope of a variable, i.e. when is the variable accessible/valid. A variable declare in main() is not accessible in the function called from main().

So in each of your function you need to open the file again. Something like:

ifstream infile;
infile.open(filename);
if (!infile)
    throw "file cannot be opened";

in all three functions.

And then the getline – use

getline(infile,spaces))

instead.

Leave a Comment