0

I'm trying to read a text file via standard input but I'm not entirely sure where to begin when there's an unknown amount of data. I'm somewhat familiar with using getline from a text with a given amount of data. When reading a text file with a known data size I would just use something like

char file[250];
while (cin.getline(file,250)){ //etc
}

However when I don't know what to put in either of the parameters I'm pretty much lost. How should I approach this? Should I be using a different function instead? Thanks.

  • 1
    Use `std::getline` to read into a `string` rather than a fixed size buffer. http://www.cplusplus.com/reference/string/string/getline/ – Jonathan Potter Dec 05 '15 at 02:02

1 Answers1

2

To do this all you need to do is:

string line;
while(getline(cin, line) {
   //process line
}

This will read until you hit eof or an error

FrozenHawk
  • 150
  • 2
  • 8