C++ : Dynamic C-String Usage in ifstreamObject.getline(c string, char limit)

120 views Asked by At

I was wondering if there is a way to dynamically allocate space to a char aray equal to the amount of spaces in the line from the file while use the getline method. (C++)

example

int main(){    
    char *theLine;
    ifstream theFile;

    //set theLine = new char[sizeOftheFileLine]
    //is there a way do the above
    theFile.getline(theLine, 500);
    return 0;
}
1

There are 1 answers

0
R Sahu On BEST ANSWER

If you use std::getline, you get the desired behavior.

std::string theLine;
std::ifstream theFile;

// ....

std::getline(theFile, theLine);