fork(1) download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <iterator>
  4.  
  5. int main() {
  6. char buffer[7];
  7. std::istream & file = std::cin;
  8.  
  9. while (true) {
  10. file.getline(buffer, sizeof buffer);
  11. auto n = file.gcount();
  12.  
  13. if (file) {
  14. std::cout << "Read line with " << n << " characters: '";
  15. std::copy_n(buffer, n - 1, std::ostream_iterator<char>(std::cout));
  16. std::cout << "'\n";
  17. } else if (n > 0) {
  18. std::cout << "Read incomplete line with prefix '";
  19. std::copy_n(buffer, n - 1, std::ostream_iterator<char>(std::cout));
  20. std::cout << "'.\n";
  21. file.clear();
  22. } else {
  23. std::cout << "Did not read any lines.\n";
  24. break;
  25. }
  26. }
  27. }
Success #stdin #stdout 0s 15232KB
stdin
Foo
FooBar
ThisLineIsTooLong
stdout
Read line with 4 characters: 'Foo'
Read line with 7 characters: 'FooBar'
Read incomplete line with prefix 'ThisL'.
Read incomplete line with prefix 'neIsT'.
Read line with 5 characters: 'oLon'
Did not read any lines.