Read long lines from cin?
C / C++ / MFC
2
Posts
2
Posters
0
Views
1
Watching
-
-
Hi Could someone tell me how I can get an arbitrary-length line from std::cin without fear of buffer overrrun, while still accomodating very long lines? I can use cin.getline(), but that way I have to define a buffer with a fixed size first. Thanks!
#include <iostream>
#include <string>int main() {
std::string line;
std::getline(std::cin, line);
std::cout << line << '\n';
}That should do it.
std::getline
is a mightily useful function that works on any and all input streams. It resides in<string>
. If you want to use an alternate "line separator" there is an overloadedstd::getline
for this as well. Hope it helps. -- Henrik Stuart (http://www.unprompted.com/hstuart/[^])