The approach you use will not work - when cin gives you the end of the file in the context you use, then cin closes.
For your stated purpose: “read the text until it repeats”, sorry for the lack of nuance of this earlier, but if you clone the stdin file descriptor and then use the clone, you can continue reading from these additional file descriptors.
Cloning iostreams is not easy. See How to build C ++ fstream from a POSIX file descriptor?
This is a bit like c-like, but this code flushes one copy of stdin until that stdin closes, then it makes a new copy and merge it further.
#include <iostream> #include <string> void getInput(std::string& input) { char buffer[4096]; int newIn = dup(STDIN_FILENO); int result = EAGAIN; input = ""; do { buffer[0] = 0; result = read(newIn, buffer, sizeof(buffer)); if (result > 0) input += buffer; } while (result >= sizeof(buffer)); close(newIn); return input; } int main(int argc, const char* argv[]) { std::string input; for (;;) { getInput(input); if (input.empty()) break; std::cout << "8x --- start --- x8\n" << input.c_str() << "\n8x --- end --- x8\n\n"; } }
source share