Search code examples
c++fstreamifstream

Using .getline with string instead of char


So I have some code here that reads from a text document and stores it into char bunz. I know this sounds like a stupid question but I'd rather use string instead of char. Will .getline accept a string if it is used with ifstream? Or will I be forced to convert the char to a string afterwords?

Thanks.

ifstream filler("C:\\bunz.txt");

char bunz[30+1];
filler.getline(bunz, 40);
cout<<bunz;
filler.close();

Solution

  • Notorious for posting answers as comments, chris is spot on. Once you use std::getline(), you'll never go back:

    ifstream filler("C:\\bunz.txt");
    string bunz;
    getline(filler, bunz);
    cout<<bunz;
    filler.close();