fork(2) download
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. void test(const string &data) {
  7. istringstream file(data);
  8. string line;
  9. int count = 0;
  10.  
  11. while (getline(file, line)) {
  12. ++count;
  13. if (file.eof()) break;
  14. }
  15. if (file.eof() && file.fail()) {
  16. ++count;
  17. }
  18.  
  19. cout << "lines: " << count << endl;
  20. }
  21.  
  22. int main() {
  23. test("line");
  24. test("line\n");
  25. test("line\nline");
  26. test("line\nline\n");
  27. return 0;
  28. }
Success #stdin #stdout 0s 4796KB
stdin
Standard input is empty
stdout
lines: 1
lines: 2
lines: 2
lines: 3